如何使用bloc显示ListView.builder

huangapple go评论44阅读模式
英文:

How to show ListView.builder with bloc

问题

我想要使用ListView.builder显示物品列表。我的代码成功连接到API,但问题在于我不知道如何显示物品,而不只是显示物品的长度。

这是我附上的代码:

                  child: ListView.builder(
                        itemCount: state.Food.length,
                        itemBuilder: (context, index) {
                          return Row(
                            children: [
                              const SizedBox(
                                height: 10,
                                width: 10,
                                child: CircleAvatar(
                                  foregroundColor:
                                      ColorName.brandSecondaryGreen,
                                  backgroundColor:
                                      ColorName.brandSecondaryGreen,
                                ),
                              ),
                              const SizedBox(
                                width: 5,
                              ),
                              Text(
                                state.Food.length.toString(), //问题出在这里----------
                                style: subtitle1(),
                              ),
                            ],
                          );
                        },
                      );
                    },
                  ),
                ),
              ),
英文:

I want to showing list of items with ListView.builder. My code is success connect to API, but the problem is I do not know how to display the items, instead the length of the items.

Here I attach the code:

                  child: ListView.builder(
                        itemCount: state.Food.length,
                        itemBuilder: (context, index) {
                          return Row(
                            children: [
                              const SizedBox(
                                height: 10,
                                width: 10,
                                child: CircleAvatar(
                                  foregroundColor:
                                      ColorName.brandSecondaryGreen,
                                  backgroundColor:
                                      ColorName.brandSecondaryGreen,
                                ),
                              ),
                              const SizedBox(
                                width: 5,
                              ),
                              Text(
                                state.Food.length.toString(), //The problem is here----------
                                style: subtitle1(),
                              ),
                            ],
                          );
                        },
                      );
                    },
                  ),
                ),
              ),

答案1

得分: 1

使用列表生成器的索引而不是这个:

Text(
   state.food.length.toString(),
   style: subtitle1(),
),

使用列表生成器的索引:

Text(
   state.food?[index].attributeName ?? '没有数值',
   style: subtitle1(),
),
英文:

Instead of this

> Text(
> state.food.length.toString(),
> style: subtitle1(),
> ),

use the index of the list builder

> Text(
> state.food?[index].attributeName ?? 'No value',
> style: subtitle1(),
> ),

答案2

得分: 1

当您遍历ListView时,它提供了index来访问正在迭代的项目的属性,使用index您可以显示该项目。

示例:

 Text(state.Food[index].propName, // 👈 将propName更改为Food模型的任何属性
   style: subtitle1()),
英文:

As you are iterating over the Listview, it gives you index to access the property of the individual iterated item , using index you can display the item.

Example:

 Text(state.Food[index].propName, // 👈 change the propName to any property of Food model
   style: subtitle1()),

huangapple
  • 本文由 发表于 2023年2月16日 17:11:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469989.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定