变量未更新.map()中的值

时间:2020-07-27 21:21:37

标签: flutter dart

我正在学习使用Flutter,因此正在关注所发现的教程。 在那里,它使用.map函数读取列表并将其关联到最终变量。问题是,当我尝试在.map函数中使用变量时出现错误。我认为是由于该功能的范围,但是在本教程中它可以正常工作。

这是我的代码:

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 这就是我的代码

enter image description here 这是我得到的错误

能否请您解释一下为什么它不使用变量?我该如何解决?

感谢您的关注和帮助。

1 个答案:

答案 0 :(得分:1)

;后面的ListTile(...)代替,

相关问题