missing_default_value_for_parameter
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-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}) {}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。