Closed as duplicate of#1536
Description
Here is a condensed example of the problem.
typedef Func = int Function(int);
Func weird(Func? functor) {
functor ??= (_) => 42; // functor cannot be null hereafter
return (int i) {
return functor!(i); // yet, why is ! needed here?
};
}
Even moving the promotion inside the returned function fails:
typedef Func = int Function(int);
Func weird(Func? functor) {
return (int i) {
functor ??= (_) => 42; // functor cannot be null hereafter
return functor!(i); // yet, why is ! needed here?
};
}
But this works:
Func weird(Func? functor) {
functor ??= (_) => 42; // functor cannot be null hereafter
return functor; // <-- this works;
// return functor!; // <-- this correctly warns that ! will have no effect
}
and this as well:
Func weird(Func? functor) {
final f = functor ?? (_) => 42; // f cannot be null
return (int i) {
return f(i); // Tada 🎉
};
}
dart info
#### General info
- Dart 3.7.2 (stable) (Tue Mar 11 04:27:50 2025 -0700) on "macos_x64"
- on macos / Version 15.4 (Build 24E248)
- locale is en-DK
#### Project info
- sdk constraint: '^3.7.2'
- dependencies:
- dev_dependencies: lints, test