Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate over a List of objects in Dart and present properties of object as a list in flutter

Tags:

flutter

dart

    array arr = [MenuItem1, MenuItem2, MenuItem3]

    class MenuItem{
        String desc;
        String id;
    }

For the above class and array, how would I present them as a dynamic list flutter?

Rows of Row(chidren[Text((desc),Text((id)]);

like image 737
flutter Avatar asked Aug 31 '25 17:08

flutter


1 Answers

After Dart 2.3 you can use Collection For:

return Row(children:
[for (MenuItem item in arr ) Text(item.desc)]
);

Since you want to return 2 Widgets for each item in the array, you can combine this with the Spread operator:

return Row(children:
    [for (MenuItem item in arr ) ...[Text(item.desc),Text(item.id)]]
);
like image 71
Naslausky Avatar answered Sep 02 '25 08:09

Naslausky