Home > Net >  a value of color can't be assigned to a type color
a value of color can't be assigned to a type color

Time:12-04

enter image description here

I am getting an error how can I solve this.

CodePudding user response:

You need to replace Colors.indigo[900] with Colors.indigo[900]!:

Color bgColor = data['isDayTime'] ? Colors.blue : Colors.indigo[900]!;

Color.indigo is a MaterialColor and the returned type of the operator [] on a MaterialColor is Color?. It means it can return a Color, but also null.

If you know that Colors.indigo[900] is not null. Then you can use the null check operator (!) to tell dart that you know the variable Colors? Colors.indigo[900] is not null and it is in fact a Color.

  • Related