You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I tried to migrate my flutter code to a null safety version and came across the following problem:
class MyClass {
final String? myString;
MyClass(this.myString);
}
String? f(String input) {
return input;
}
void main(){
final c = MyClass("string");
String? s = c.myString != null ? f(c.myString) : null;
}
which gives me the following compilation error:
lib/main.dart:13:40:
Error: The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't.
String? s = c.myString != null ? f(c.myString) : null; // This
^
I can easily change this to an equivalent code by extracting c.myString to a new variable which works fine:
class MyClass {
final String? myString;
MyClass(this.myString);
}
String? f(String input) {
return input;
}
void main(){
final c = MyClass("string");
final myString = c.myString;
String? s = myString != null ? f(myString) : null;
}
It looks like a bug in the type promotion logic in the first example or maybe I am missing something why this couldn't be type promoted :)
The text was updated successfully, but these errors were encountered:
Unfortunately, this is working as intended. See here for some general discussion about working with fields, here for some discussion of why even final fields can't be promoted soundly, and here for a list of issues discussing potential ways of making fields easier to work with in null safety.
I tried to migrate my flutter code to a null safety version and came across the following problem:
which gives me the following compilation error:
I can easily change this to an equivalent code by extracting
c.myString
to a new variable which works fine:It looks like a bug in the type promotion logic in the first example or maybe I am missing something why this couldn't be type promoted :)
The text was updated successfully, but these errors were encountered: