在扑朔迷离中,您无法设置title
变量进行测试,因为Text
不会是恒定的。
class StockCard extends StatelessWidget {
String test;
StockCard(String t) {
test = t;
}
@override
Widget build(BuildContext context) {
return (
new Container(
margin: const EdgeInsets.all(10.0),
child: new Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const ListTile(
leading: Icon(Icons.trending_up),
title: Text(test), // ERROR: error: Evaluation of this constant expression throws an exception.
subtitle: Text('Apple Incorporated'),
),
],
),
),
)
);
}
}
我将如何实现这一目标?为什么颤振不允许这种情况发生?
答案 0 :(得分:2)
之所以发生这种情况,是因为您尝试将父项ListTile
实例化为const
。
但是要使ListTile
为const
,它也需要其子Text
为const,因此不能有变量。
只需将ListTile
更改为 not 为const
:
ListTile(
...
)