Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List<MyType> to <Widget>

Tags:

flutter

dart

I am trying to understand the casting system of Dart, in specific with flutter.

I have a List of my custom type I would like to fill in a TableRow, but I simply fail to use map for it. Can anyone help me to point out what my problem is? At the end I need a list of widgets for the children parameter.

TableRow(
    children: myList.map((MyType foo) {
            return <Widget>[new Text(foo.name)];
        }) as List<Widget>
),

The error I am getting is:

flutter: type 'MappedListIterable<MyType, List<Text>>' is not a subtype of type 'List<Widget>' in type cast

Any help is highly appreciated

like image 320
Daniel Stephens Avatar asked Feb 12 '26 10:02

Daniel Stephens


1 Answers

Just do this:

TableRow(
    children: myList.map((foo) {
            return new Text(foo.name);
        }).toList(),
),

As Rémi says, you need to return a widget inside the map() function, not a list of widgets. And then convert the iterable to a List with toList() method.

like image 85
chemamolins Avatar answered Feb 15 '26 01:02

chemamolins



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!