Variable is not updating the value inside a .map()

I'm learning to use Flutter so I'm following a tutorial I found. There it uses a .map function to read a list and assing it to a final variable. The thing is that I get an error when I'm trying to use a variable inside the .map function. I think it is because of the scope of the function, but in the tutorial it works properly.

Here is my code:

List<Widget> _listaItems( List<dynamic> data ) {

   final List<Widget> listaWid = [];               // Here I'm creating the variable listaWid

   data.forEach((opt) {

     final widgetTemp = ListTile(
       title: Text(opt['texto']),
       leading: Icon(Icons.account_circle, color: Colors.deepOrange,),
       trailing: Icon(Icons.keyboard_arrow_right),

     ),

     listaWid.add(widgetTemp);                      // here it show the error 'listaWid must be inizialized'
     listaWid.add(Divider());                       // here it show the error 'listaWid must be inizialized'

   });

   return listaWid;


}

enter image description here This is how I have my code

enter image description here This is the error I get

Can you please explain me why it is not taking the variable? How can I fix it?

Thanks for your attention and help.

Upvotes: 0

Views: 246

Answers (1)

mohamed zaitoon
mohamed zaitoon

Reputation: 26

Put ; after ListTile(...) instead of ,

Upvotes: 1

Related Questions