Search code examples
flutterdartbloc

How to show ListView.builder with bloc


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(),
                              ),
                            ],
                          );
                        },
                      );
                    },
                  ),
                ),
              ),

Solution

  • 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(),
    ),