How can I assign type 'double?' to a double?

Viewed 55

I'm trying to write a config file and I have the following code:

const config = const Config();

class Config {
  final Map<String, double> blur;

  const Config({this.blur: const {'sigmaX': 50.0, 'sigmaY': 50.0}});
}

And I'm trying to use config.blur['sigmaX'] in another file for the use of ImageFilter like this:

filter: ImageFilter.blur(sigmaX: config.blur['sigmaX'], sigmaY: 50),

But I get the following error when I trying to build and run it:

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

Null Safety is on.

1 Answers

You have a value that might be null, and the blur operator can't take a null value for the sigmaX parameter. You can use the ?? (null coalescing) operator to provide a default value for the case where config.blur['sigmaX'] happens to be null:

filter: ImageFilter.blur(sigmaX: config.blur['sigmaX'] ?? 50, sigmaY: 50),
Related