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}) {}
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-4. View source or report an issue.