【问题标题】:how to set the array list in a listview builder in flutter?如何在颤动的列表视图构建器中设置数组列表?
【发布时间】:2020-07-18 04:15:34
【问题描述】:

我已成功从 firestore 数据库中获取数组,我正在尝试使用 ListView.builder 为数组构建一个列表,但无法这样做。

这是我的数组列表

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

列表视图构建器的代码

                   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}",
                                        

                                    ],
                                  ),
                                ),
                              ),
                            ),

这是我得到的结果的截图

我希望以序列化方式索引获取的数组列表。我应该如何实现它? 这就是我希望列表的显示方式

  1. ffg
  2. fgsgg
  3. fgfda

【问题讨论】:

  • 您希望它如何显示尚不清楚。明确你想要的布局。
  • @JigarPatel 我已经更新了上面的问题以显示列表,它在问题的最后部分
  • 你能说明什么是widget.steps吗?
  • @Haryanvi 是的,它是问题上述部分中的数组列表
  • 好的。希望你得到答案。

标签: arrays flutter flutter-listview


【解决方案1】:

您需要在构建器中引用数组中的索引。 widget.steps 使用List 的默认toString 方法将其转换为String

如果您不想使用默认的 toStringMap(这是每个 List 引用中包含的内容),请同时引用您要显示的 Map 项目。

这些对象的两个引用运算符都是[]

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
        ],
      ),
    ),
  ),
),

【讨论】:

    【解决方案2】:

    你可以像这样使用Column..

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

    ..或者像这样使用ListView.builder..

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

    【讨论】:

      【解决方案3】:

      从您的回复中,widget.steps 是 Map 类型的列表,以便获取列表中的内容:

      1. 你必须定义索引号
      2. 选择键以获取其值

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

      【讨论】:

        【解决方案4】:

        随便用

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

        【讨论】:

          猜你喜欢
          • 2019-04-08
          • 1970-01-01
          • 1970-01-01
          • 2018-10-26
          • 2020-11-20
          • 1970-01-01
          • 2022-01-16
          • 2020-09-20
          • 1970-01-01
          相关资源
          最近更新 更多