flutter
flutter

Reputation: 6766

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

    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)]);

Upvotes: 4

Views: 7824

Answers (2)

Alpesh Rathod
Alpesh Rathod

Reputation: 271

This code will help you to update the list dynamically.

 ListView.builder(
        itemCount: arr.length,
        itemBuilder: (_, index) {
          return Row(
            children: <Widget>[Text(arr[index].desc), Text(arr[index].id)],
          );
        },
      )

Upvotes: 2

Naslausky
Naslausky

Reputation: 3768

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)]]
);

Upvotes: 5

Related Questions