Asmoun
Asmoun

Reputation: 1747

flutter widgets inside for loop

I have the following code to create Row widgets based on a returned items from Future Builder

 for (int i = 0; i < snapshot.data.length; i++)  Row( ) ,

the above will create Rows based on the number of items from snapshot.data what I'm trying to achieve is create multiple widget based on the length of items returned

for (int i = 0; i < snapshot.data.length; i++) {
    // for each item create two rows for example 
      Row( ) ,
      Row( ) ,
    } 

but I get the following error !

the element Type 'set<Row>' can't be assigned to the list 'Widget'

Upvotes: 1

Views: 319

Answers (3)

Luis Utrera
Luis Utrera

Reputation: 1067

You either use a listView.builder or for in. When you're building a widget you can't use curly braces

Upvotes: 1

JahidRatul
JahidRatul

Reputation: 519

Use ListView.builder defining the length to itemCount property. For Example..

body: ListView.builder
  (
    itemCount: snapshot.data.length,
    itemBuilder: (BuildContext ctxt, int index) {
     return Row();
    }
  )

Upvotes: 1

Mohammad_Asef
Mohammad_Asef

Reputation: 336

Try this one:

List.generate(snapshot.data.length, (index) => Row( ),),

or if you want to insert deferent widget at deferent index use:

List.generate(snapshot.data.length, (index) {
   if(index == 0){return Row();}
   else if(index == 1){return Column();}
   else{return Container();}
),

Upvotes: 1

Related Questions