Ojasv Singh
Ojasv Singh

Reputation: 51

The element type 'List<Card>' can't be assigned to the list type 'Widget'

I don't understand how can we use the column widget after removing the square brackets[] from the children. I wanted to create a list of cards from a list of string elements. I used the map method to do so.

I was using the map method inside the column widget and it constantly showed the error The element type 'List<Card>' can't be assigned to the list type 'Widget'. I solved it by wrapping the flights.map((element){}) inside another column widget and then removing the [] of the children.

Column(
              children:[ flights.map(
                    (element) => Card(
                  child: Column(
                    children: <Widget>[
                      Image.asset('images/flight.jfif'),
                      Text(element)
                    ],
                  ),
                ),
              ).toList(),]
            )
          ],
)

afterwards

Column(
              children: flights.map(
                    (element) => Card(
                  child: Column(
                    children: <Widget>[
                      Image.asset('images/flight.jfif'),
                      Text(element)
                    ],
                  ),
                ),
              ).toList(),
            )
          ],
        )

The first code snippet shows the error that The element type 'List' can't be assigned to the list type 'Widget'.

The second one does not show any error

Upvotes: 3

Views: 9365

Answers (1)

amangautam1
amangautam1

Reputation: 850

Since children requires list of widgets. But in your first case it is getting list of list because flights.map will return list of Card widgets. so it becomes like,

Column(
       children:[ []
    ],
)

But in second case, children is getting only list of widgets that's what it needed. So it becomes,

Column(
       children: []  
)

which is right format.

Upvotes: 5

Related Questions