In flutter you can't set the title variable to test because Text would not be constant.
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'),
),
],
),
),
)
);
}
}
How would I achieve this and why does flutter not allow this to happen?
This happens because you try to instantiate the parent ListTile as const.
But for ListTile to be const, it requires its child Text to be const too, therefore cannot have a variable.
Just change ListTile to not be const:
ListTile(
...
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With