如何解决颤振应用程序中的多个列表构建器
我能够按照以下代码在 Flutter 应用程序中成功创建一个 listview.builder。
代码
Container(
child: StreamBuilder<QuerySnapshot>(
stream: query2.snapshots(),builder: (BuildContext context,AsyncSnapshot<QuerySnapshot> snapshot) {
var usernames = snapshot.data.docs.map((e) => e['itemName']);
print("usernames");
print(usernames);
if (snapshot.hasError) {
return Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return ListView.builder(
itemCount: snapshot.data.docs.length,itemBuilder: (context,index){
// String itemname =snapshot.data.docs[index]['itemName'] ?? "";
return ListTile(title:Text(snapshot.data.docs[index]['itemName'] ?? ""
),);
});
}
),)
但我想在大约 5 种不同的 if-else 条件下创建列表视图构建器,但我无法这样做,我尝试在 StreamBuilder
上实现此功能,但无法实现三元运算符有效,但一次仅适用于两个条件而不是多个条件,我应该如何实现?
解决方法
这很简单。
if(condition1)
return Container(color:Colors.blue);
else if(condition2)
return Container(color:Colors.yellow);
else if(condition3)
return Container(color:Colors.green);
else if(condition4)
return Container(color:Colors.red);
...etc.
else
return SizedBox();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。