List<MaterialColor?> changeColor = [
Colors.red,
Colors.blue,
Colors.yellow,
Colors.green,
Colors.pink,
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'Mohammed is coming back ',
style: TextStyle(
color: changeColor[_counter],
fontSize: 22,
fontWeight: FontWeight.bold,
),
),
I made a list consisting of a set of colors and I want to give it to the color but it keeps giving me this error, even though I gave the list type the same as the color type
CodePudding user response:
_counter
value will change and get on read time. That's why Text
widget can not be const. Remove const from Text.
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Mohammed is coming back ',
style: TextStyle(
color: changeColor[_counter],
fontSize: 22,
fontWeight: FontWeight.bold,
),
),
],
),
),
You can more about
- const on dart.dev and
- a good question on difference between the "const" and "final" keywords in Dart?