Render list flutter

Iterating through a list to render multiple widgets in Flutter?

Basically when you hit 'return' on a function the function will stop and will not continue your iteration, so what you need to do is put it all on a list and then add it as a children of a widget

you can do something like this:

Widget getTextWidgets[List strings] { List list = new List[]; for[var i = 0; i < strings.length; i++]{ list.add[new Text[strings[i]]]; } return new Row[children: list]; }

or even better, you can use .map[] operator and do something like this:

Widget getTextWidgets[List strings] { return new Row[children: strings.map[[item] => new Text[item]].toList[]]; }

It is now possible to achieve that in Flutter 1.5 and Dart 2.3 by using a for element in your collection.

var list = ["one", "two", "three", "four"]; child: Column[ mainAxisAlignment: MainAxisAlignment.center, children: [ for[var item in list ] Text[item] ], ],

This will display four Text widgets containing the items in the list.
NB. No braces around the for loop and no return keyword.


The Dart language has aspects of functional programming, so what you want can be written concisely as:

List list = ['one', 'two', 'three', 'four']; List widgets = list.map[[name] => new Text[name]].toList[];

Read this as "take each name in list and map it to a Text and form them back into a List".

Video liên quan

Chủ Đề