Jokol
Jokol

Reputation: 37

'Iterable<Widget>' can't be assigned to the list type 'Widget'

I'm learing flutter and I tried to recreate this from fireship.io.

I copied the code and I get this Error:

The element type 'Iterable<Widget>' can't be assigned to the list type 'Widget'.

Code:

    List items = [
        MenuItem(x: -1.0, name: 'house'),
        MenuItem(x: -0.5, name: 'planet'),
        MenuItem(x: 0.0, name: 'camera'),
        MenuItem(x: 0.5, name: 'heart'),
        MenuItem(x: 1.0, name: 'head'),
    ];



Container(
            // <-- 4. Main menu row
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [items.map((item) => _flare(item))],
            ),
          ),

Class MenuItem:

class MenuItem {
  final String name; // name is the filename of the .flr asset
  final double
      x; // x is the X-axis alignment of the item (-1 is far left, 1 is far right).
  MenuItem({required this.name, required this.x});

I found a question with the same error but I couldn't solve it.

Upvotes: 1

Views: 941

Answers (2)

Midhun MP
Midhun MP

Reputation: 107131

map returns iterable, so you've to convert that to list. Also you don't need to use [] there.

Container(
   child: Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      crossAxisAlignment: CrossAxisAlignment.end,
      children: items.map((item) => _flare(item)).toList(),
   ),
),

Upvotes: 1

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63604

try this way

  children: [
            ...items.map((item) => _flare(item)).toList(),
          ],

Upvotes: 0

Related Questions