Reputation: 141
I have a List with this structure called userList
List userLIst = [{NAME: John, SURNAME: Doe, DATE: 24/8/2021, DESCRIPTION: Some words}, {...}, {...}];
I want to refer to that list and use List.generate(userList.length, index) => Container(display data from the list), so I can display all the data inside the cards. Is it possible to do this?
Upvotes: 0
Views: 1300
Reputation: 1144
Yes it's possible. you have to loop through the list generate widgets based on each map data, like this:
for (var i in userLIst)
Column(children:[
Text(i.keys)
Text(i.values),
])
I used for loop instead of List.generate. refer here for more information.
Upvotes: 1
Reputation: 519
You should use the map method for Lists:
final widgetList = userList.map((element) => Container(child: Text(element['NAME']))).toList();
than you can display this list in a ListView
or Column
Widget in build method.
class YourView extends StatelessWidget{
Widget build(){
final widgetList = userList.map<Widget>((element) => Container(child:
Text(element['NAME']))).toList();
return ListView(
children: widgetList,
);
}
}
Upvotes: 2