Android 为什么可以';我不能在《颤栗》中为标题引入一个变量吗?
在flatter中,您不能将Android 为什么可以';我不能在《颤栗》中为标题引入一个变量吗?,android,dart,flutter,Android,Dart,Flutter,在flatter中,您不能将title变量设置为test,因为Text不是常量 class StockCard extends StatelessWidget { String test; StockCard(String t) { test = t; } @override Widget build(BuildContext context) { return ( new Container( margin: const E
title
变量设置为test,因为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'),
),
],
),
),
)
);
}
}
class StockCard扩展了一个小部件{
串试验;
股票卡(字符串t){
试验=t;
}
@凌驾
小部件构建(构建上下文){
返回(
新容器(
边距:所有常数边集(10.0),
孩子:新卡(
子:列(
mainAxisSize:mainAxisSize.min,
儿童:[
常量列表(
前导:图标(图标。趋势向上),
title:Text(test),//错误:错误:此常量表达式的计算引发异常。
字幕:文本(“苹果公司”),
),
],
),
),
)
);
}
}
我将如何实现这一点,为什么颤振不允许这种情况发生?之所以会发生这种情况,是因为您试图将父级
listile
实例化为const
但是对于listile
为const
,它要求其子Text
也为const,因此不能有变量。
只需将listile
更改为非const
:
ListTile(
...
)