The code below fails when compiling with Null Safety the following error:
The parameter namedParam
can't have a value of null
because of its type, and no non-null default value is provided.
void main() {
Foo(callbackWithNamedParam: ({namedParam}) {
print('param=$namedParam');
}).callback();
}
class Foo {
final void Function({required int namedParam}) callbackWithNamedParam;
Foo({required this.callbackWithNamedParam});
void callback() {
callbackWithNamedParam(namedParam: 10);
}
}
When compiling without Null Safety the code compiles perfectly well.
DartPad with Null Safety that fails compiling
DartPad without Null Safety that compiles well
Is there a way to fix it:
- Without switching to positional parameter
AND - Without making the named parameter
nullable
?