flutter 如何显示带有块的ListView.builder

x3naxklr  于 2023-02-16  发布在  Flutter
关注(0)|答案(1)|浏览(161)

我想用ListView显示项目列表。builder,它与BlocBuilder Package 。我的代码是成功连接到API,但问题是我不知道如何显示项目,而不是像下面的图片项目的长度。

在这里我附上代码:

SizedBox(
                height: 350,
                width: 290,
                child: Padding(
                  padding: const EdgeInsets.only(left: 30, top: 20),
                  child: BlocBuilder<ExcavatorBloc, ExcavatorState>(
                    builder: (context, state) {
                      return ListView.builder(
                        itemCount: state.excavator.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.excavator.length.toString(), //The problem is here----------
                                style: subtitle1(),
                              ),
                            ],
                          );
                        },
                      );
                    },
                  ),
                ),
              ),
wnavrhmk

wnavrhmk1#

而不是这个

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

使用列表生成器的索引

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

相关问题