Home > Software engineering >  Flutter iterating thorug array of objects and displaying name of each object
Flutter iterating thorug array of objects and displaying name of each object

Time:08-11

I have a list of members List<AllMembers> _clanoviZaDijeljenje = []; and I want to iterate thorugh this list and display name of each member. I'm tryng something like this _clanoviZaDijeljenje.map((member) { return Text(member.name);}); and i get error: The element type 'Iterable' can't be assigned to the list type 'Widget'.

CodePudding user response:

Remember that .map returns Iterable<T>.
Whenever you need a list you have to cast iterable to the list by adding .toList():

_clanoviZaDijeljenje.map((member) { return Text(member.name);}).toList();

CodePudding user response:

You can use foreach cycle.

First way:

_clanoviZaDijeljenje.forEach((member) return Text(member.name));

Second way:

for(Member member in _clanoviZaDijeljenje){ return Text(member.name);}
  • Related