AJ-
AJ-

Reputation: 1783

Flutter null safety - The argument type 'Color?' can't be assigned to the parameter type 'Color'

I changed my SDK version for flutter to min , so that I can fix my code for null safety.

There is one issue that I don't understand, this line produces the following error:

The argument type 'Color?' can't be assigned to the parameter type 'Color'

border: Border.all(color: Colors.grey[300], width: 1),

but if I change Colors.grey[300] with whatever value that doesn't use [], it will work, so Colors.grey works perfectly fine.

What should I change here to keep using grey[300]?

Upvotes: 14

Views: 8992

Answers (4)

Syed Hussain
Syed Hussain

Reputation: 11

You can write Colors.grey[300]! like this and this will work.

Upvotes: 1

Cedric Tanda
Cedric Tanda

Reputation: 31

use shade300 like Color color = Colors.grey.shade300;

Upvotes: 2

Simon Sot
Simon Sot

Reputation: 3136

You can use 0xFFE0E0E0 for grey[300].

To pick material colors you can use this tool.

To select a specific color from one of the swatches, index into the swatch using an integer for the specific color desired, as follows:

Color selection = Colors.green[400]!; // Selects a mid-range green.

Each ColorSwatch constant is a color and can used directly. For example:

Container(
  color: Colors.blue, // same as Colors.blue[500] or Colors.blue.shade500
)

Upvotes: 8

CopsOnRoad
CopsOnRoad

Reputation: 267604

Problem:

Color color = Colors.grey[300]; // Error in null-safe mode

When you use Colors.grey[300], you're actually getting the color from a Map which Dart (in null-safe mode) warns you about because that value could be null. See more


Solutions:

There are two solutions for it. One is general and one is only specific to this case.

  1. Use Bang operator (!)

    Color color = Colors.grey[300]!  // Added '!', error gone 
    
  2. Use shadeXXX on the Color

    Color color = Colors.grey.shade300;
    

Upvotes: 20

Related Questions