颤振列表迭代错误-无法将列表函数分配给小部件

时间:2020-06-15 16:31:53

标签: flutter dart iteration

我试图从数组中简单地打印一些数据,但我不断收到一个错误消息,说不能将列表函数分配给类型小部件,但是我检查了所有内容,每个组件看起来都应该可以,所以让我们开始吧包含数据:

class GameOptions {
  String name;
  int key;
  int hidden;
  int random;

  GameOptions({this.name, this.key, this.hidden, this.random});
}
// Populate the Data

List <GameOptions> options = [
  GameOptions(name : "Town Hall",  key : 001, hidden:1, random: 0),
  GameOptions(name : "Chapel", key : 003, hidden:1, random: 0),
  GameOptions(name : "Tavern", key : 002, hidden:1, random: 0),
  GameOptions(name : "Merchant", key : 004, hidden:1, random: 0),
] ;

然后是我调用此数据进行文本打印的代码:

            Expanded(
              flex:3,
              child: Column (
                children: options.map((name) => Text(name)).toList,
              ),
            ),

这是我得到错误的子行。预先感谢,如果是因为对语法的简单误解,或者在构建列表时遇到问题,我将无法理解。

1 个答案:

答案 0 :(得分:0)

实际上,您是将整个GameOptions对象传递到文本小部件,而不是在那儿传递其name属性:

 children: options.map((name) => Text(name)).toList,

应该是这样

 children: options.map((gameOption) => Text(gameOption.name)).toList,

只需替换这一行,您的代码即可正常运行。