HelloWorldd
HelloWorldd

Reputation: 202

type 'List<Widget>' is not a subtype of type 'Widget' in a dynamic attribution of widgets

I have this code:

child: Column(
  children: [
    Text(i.description),
    i.colors.map<Widget>(
    (colours) => CheckboxListTile(
      title: Text(colors),
      value: i[colors],
      onChanged: (colors){
        setState(() {
          i[colors] = colors;
        });
      },
    ).toList()
   ],
  )

I want to show checkbuttons for each color present in each object, but it throws this error:

type 'List < Widget>' is not a subtype of type 'Widget'

How can I fix it?

Upvotes: 1

Views: 59

Answers (1)

BabC
BabC

Reputation: 1084

You got the error because Column.children expects a 'simple' array. But you gave an array with a Text AND another array (i.colors.map<>(___).toList()), so you got your error.

Add the spread operator ... before your list, it will unwrap it :

child: Column(
  children: [
    Text(i.description),
    ...i.colors.map<Widget>(
    (colours) => CheckboxListTile(
      title: Text(colors),
      value: i[colors],
      onChanged: (colors){
        setState(() {
          i[colors] = colors;
        });
      },
    ).toList()
   ],
  )

Upvotes: 3

Related Questions