skip to Main Content
Column(
          children: [
            Widget1(),
            Widget2(),
            Expanded(
              child: SingleChildScrollView(
                child: Container(
                  child: Column(
                    children: [
                              for(var i =0 ;i<categories.length;i++){
                                DetailsCard(catName: categories[i]);
                              }
                    ],
                  ),
                ),
              ),
            ),
          ],
        ) 

In the above code i want to add multiple widgets based on the items in the categories list . But when I try using the for loop i keep getting "The element type ‘Set’ can’t be assigned to the list type ‘Widget’." error.

2

Answers


  1. use for statement inside of [...] like below:

    Column(
              children: [
                Widget1(),
                Widget2(),
                Expanded(
                  child: SingleChildScrollView(
                    child: Container(
                      child: Column(
                        children: [
                                  for(var i =0 ;i<categories.length;i++)
                                    DetailsCard(catName: categories[i])
                        ],
                      ),
                    ),
                  ),
                ),
              ],
            ) 
    

    do not add {} and ; in [...] statement, it is illegal

    Login or Signup to reply.
  2. What I usually like to do with iterables is using .map() on Iterables (List, Sets). For example you could use:

    Column(
       children: categories.map((category) => Text(category)).toList(),
    ),
    

    If you have other Widgets in the Column other than the Widgets made from the iterables you could use the spread operator ....

    Column(
      children: [
         ...categories.map((category) => Text(category)),
         Text('This is a text'),
      ],
    ),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search