我正在尝试学习 Google Flutter,但在尝试将数据传递给 child 时遇到问题。
现在,我有两个小部件:一个应该显示口袋妖怪列表,另一个代表一个口袋妖怪。
在 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
=> new Text
。同样的事情发生了:(
最佳答案
我也遇到过这个问题。 主要问题是您将“ListTile”定义为“const”。删除“const”应该可以使您的代码正常工作。
关于dart - 将数据传递给 flutter child 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48336122/