Home > Software engineering >  There is an error even though the value type has no error (Invalid constant value)
There is an error even though the value type has no error (Invalid constant value)

Time:07-05

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

  • Related