Dart Analyze logoDart Analyze/
DRT-W1471

Missing default value for non-nullable optional parameterDRT-W1471

Major severityMajor
Bug Risk categoryBug Risk

The analyzer produces this diagnostic when an optional parameter, whether positional or named, has a potentially non-nullable type and doesn't specify a default value. Optional parameters that have no explicit default value have an implicit default value of null. If the type of the parameter doesn't allow the parameter to have a value of null, then the implicit default value isn't valid.

Examples

The following code produces this diagnostic because x can't be null, and no non-null default value is specified:

void f([int x]) {}

As does this:

void g({int x}) {}

Common fixes

If you want to use null to indicate that no value was provided, then you need to make the type nullable:

void f([int? x]) {}
void g({int? x}) {}

If the parameter can't be null, then either provide a default value:

void f([int x = 1]) {}
void g({int x = 2}) {}

or make the parameter a required parameter:

void f(int x) {}
void g({required int x}) {}