我正在尝试学习Google Flutter,并且在尝试将数据传递给孩子时遇到了问题。
现在,我有两个小部件:一个应该显示一个小宠物列表,一个代表一个小宠物。
从ListPokemon中,我使用:
创建每一行 List<Pokemon> _createRow() {
return this
._pokemons
.map((pokemonData) => new Pokemon(pokemonName: 'Super pokemon name'))
.toList();
}
在PokemonCard中,我试图制作类似的东西:
class Pokemon extends StatelessWidget {
Pokemon({Key key, this.pokemonName}) : super(key: key);
final String pokemonName;
@override
Widget build(BuildContext context) {
print(pokemonName); // prints the argument
return new Card(
child: new Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const ListTile(
leading: const Icon(Icons.album),
title: const Text(pokemonName), // gives : Arguments of constant creation must be constant expressions
subtitle: const Text('That is a weird pokemon :O'),
),
],
),
);
}
}
我的问题是这里出现了问题:
title: const Text(pokemonName), // gives : Arguments of constant creation must be constant expressions
我不明白为什么。
我想要的只是将一个字符串传递给子窗口小部件并将其显示在屏幕上。
有人可以帮我理解这个错误吗?
编辑:我试图移动const Text
=&gt; new Text
。同样的事情发生了:(
答案 0 :(得分:0)
不是添加const
小部件,而是使用new
实例化它们。由于您拥有动态内容,因此无论如何都不能const
:
children: <Widget>[
new ListTile(
leading: const Icon(Icons.album),
title: new Text(pokemonName),
subtitle: const Text('That is a weird pokemon :O'),
),
],
答案 1 :(得分:0)
我也遇到了这个问题。 主要问题是您将“ ListTile”定义为“ const”。删除“ const”应该可以使您的代码正常工作。