missing_ default_ value_ for_ parameter
Details about the 'missing_default_value_for_parameter' diagnostic produced by the Dart analyzer.
The parameter '{0}' can't have a value of 'null' because of its type, but the implicit default value is 'null'.
With null safety, use the 'required' keyword, not the '@required' annotation.
Description
#
The analyzer produces this diagnostic when an optional
parameter, whether positional or named, has a
potentially non-nullablePotentially non-nullableA type that is either non-nullable explicitly or due to
being a type parameter.
Learn more
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}) {}
除非另有说明,文档之所提及适用于 Dart 3.12.2 版本报告页面问题.