【问题标题】:multiple list builder in flutter application颤振应用程序中的多个列表构建器
【发布时间】:2021-05-30 17:46:55
【问题描述】:

我可以按照以下代码在 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 上实现这个东西,但无法做到,尽管三元运算符工作,但一次只有两个条件而不是多个条件,我应该如何实现它?

【问题讨论】:

    标签: android flutter listview dart flutter-listview


    【解决方案1】:

    这很简单。

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-05
      • 2021-01-23
      • 1970-01-01
      • 2020-06-13
      • 2020-05-11
      • 2021-02-18
      • 2020-04-14
      • 2020-07-20
      相关资源
      最近更新 更多