英文:
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()),
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论