Closed
Description
I couldn't find an issue about this, but maybe it has already been mentioned.
The below code doesn't compile:
class Tester {
void Function()? myFunc;
Tester(this.myFunc);
void test() {
if(myFunc != null) {
// ERROR: The function can't be unconditionally invoked because it can be 'null'.
myFunc();
}
}
}
The workaround of course:
myFunc!();
It seems like the null-check if-statement should resolve it, just like when using a variable of any other type, but it doesn't work with type Function, but works with other types:
class Tester2 {
String? name;
void test() {
if(name != null) {
// Compiles fine.
print(name);
}
}
}
Thanks
Metadata
Metadata
Assignees
Labels
No labels