简体   繁体   English

如何在 flutter 的列表视图构建器中设置数组列表?

[英]how to set the array list in a listview builder in flutter?

I have array fetched from firestore database successfully, for which I am trying to build a list for array using ListView.builder but not able to do so.我已成功从 firestore 数据库中获取数组,为此我正在尝试使用ListView.builder为数组构建一个列表,但无法这样做。

here is the array list I have这是我的数组列表

[{step: ffg}, {step: fgsgg}, {step: fgfda}]

code for list view builder列表视图生成器的代码

                   Expanded(
                              child: ListView.builder(
                                itemCount: widget.steps.length,
                                itemBuilder: (context, index) => Container(
                                  
                                  child: Row(
                                    crossAxisAlignment: CrossAxisAlignment.center,
                                    children: <Widget>[
                                      
                                      Text(
                                        //"STEPS ${index + 1}",
                                        "${widget.steps}",
                                        

                                    ],
                                  ),
                                ),
                              ),
                            ),

Here is the screenshot of the result I am getting这是我得到的结果的截图结果截图

I want the list of array fetched in a serialized manner index wise.我希望以序列化方式索引获取的数组列表。 How am I suppose to achive it?我应该如何实现它? This is how I want the list to be displayed这就是我希望列表显示的方式

  1. ffg ffg
  2. fgsgg fgsgg
  3. fgfda fgfda

You can do it like this using a Column ..您可以使用Column来做到这一点。

Column(
      children: widget.steps.map((item){
        return Text('${item['step']}');
      }).toList(),
    )

..or using a ListView.builder like this.. ..或使用这样的ListView.builder ..

ListView.builder(
      itemCount: widget.steps.length,
      itemBuilder: (BuildContext context, int index){
        return Text('${widget.steps[index]['step']}');
      },
    )

You need to reference an index within the array in the builder.您需要在构建器中引用数组中的索引。 widget.steps is using the default toString method of List to put convert it to a String . widget.steps使用List的默认toString方法将其转换为String

And if you want to not use the default toString of Map (which is what is contained at each List reference), reference the Map item you want to show as well.如果您不想使用Map的默认toString (这是每个List引用中包含的内容),请参考您想要显示的Map项目。

Both reference operators for these object are []这些 object 的两个参考运算符都是[]

Expanded(
  child: ListView.builder(
    itemCount: widget.steps.length,
    itemBuilder: (context, index) => Container(
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Text(
            "${widget.steps[index]['step']}",//Reference an index & key here. This is why index is provided as a parameter in this callback
        ],
      ),
    ),
  ),
),

Just use只需使用

Text( "${widget.steps[index]['step']}",)
                                        

                           

From your response widget.steps is a List of type Map so to get what's inside the list:从您的响应widget.steps是 Map 类型的列表,以便获取列表中的内容:

  1. you have to define the index number你必须定义索引号
  2. select the key to get its value select 获取其值的关键

Try this: Text("${widget.steps[index][step]}")试试这个: Text("${widget.steps[index][step]}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM