简体   繁体   中英

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'

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

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();}
),

Use ListView.builder defining the length to itemCoun t property. For Example..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM