'for loop inside widget in flutter

How do I use a loop inside a widget?

I want to be able to:

RichText(
 text: TextSpan(
 text: 'Itemlist\n',
 children: <TextSpan>[

     
   for(i = 0; i< items.length; i++){

     TextSpan( text: 'item' +i +': ',),
     TextSpan( text: 'item[i].value' +i +'\n',),


     }
 
  ]
)

it is importaint that i could have several lines of code inside the loop.



Solution 1:[1]

You can achieve by spread operator which unfold the Iterable<Widget> as follows:

RichText(
  text: TextSpan(
    text: 'Items\n',
    children: <TextSpan>[
      for(var i = 0; i< items.length; i++)
        ...[
          TextSpan( text: 'Item ${i.toString()}: '),
          TextSpan( text: '${items[i]} \n',),
        ],
    ],
  ),
)

You can check it out in DartPad also.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 saw