skip to Main Content

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

2

Answers


  1. 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(),
    ),
    
    Login or Signup to reply.
  2. 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()),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search