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
{{ message }}
Repository navigation
"Create function '...'" is missing ? for nullable variables when invoked in a class method #48086
classA {
voidf() {
// inside a class methodString? d ='';
myUndefinedMethod(0, bbb:1.0, ccc:'2', ddd: d);
}
}
// at the top levelString? d ='';
final a =myUndefinedMethod(0, bbb:1.0, ccc:'2', ddd: d);
Using the "create function" quick-fix on both myUndefinedMethods produces different results:
voidmyUndefinedMethod(int i, {double bbb, String ccc, String ddd}) {} // missing ? on dddvoidmyUndefinedMethod(int i, {double bbb, String ccc, String? ddd}) {}
The top level one includes the ?, but the one in the class member does not.
@bwilkerson both of these are the same fix (create_function), but it seems like invocation.argumentList.arguments.last.staticType?.nullabilitySuffix is different for each - for the top level function it's set to NullabilitySuffix.question but for the one inside the class method it's set to NullabilitySuffix.none.
I wonder if the local variable is getting some special treatment (or normal type promotion?) because it's known to never be null (that that can't apply to the top level)? Since there's also a difference when the variable is made required (it's valid in the method, but not the top level):
The reason I came across this was Dart-Code/Dart-Code#3770 - I was going to look at including the required keyword if the argument was not nullable, but it seemed like this could make that inconsistent (although, if it's only the case for literal strings in a nullable variable, perhaps it's not a real issue).
Removing the local variable d (so that it uses the top level one) unsurprisingly shows that it is the variable (and not being in the class method), and having the value as anything other than a literal string also prevents it. I'm fairly sure this is working as expected now.
Activity
DanTup commentedon Jan 6, 2022
I wonder if the local variable is getting some special treatment (or normal type promotion?) because it's known to never be null (that that can't apply to the top level)? Since there's also a difference when the variable is made
required(it's valid in the method, but not the top level):The reason I came across this was Dart-Code/Dart-Code#3770 - I was going to look at including the
requiredkeyword if the argument was not nullable, but it seemed like this could make that inconsistent (although, if it's only the case for literal strings in a nullable variable, perhaps it's not a real issue).DanTup commentedon Jan 6, 2022
Removing the local variable
d(so that it uses the top level one) unsurprisingly shows that it is the variable (and not being in the class method), and having the value as anything other than a literal string also prevents it. I'm fairly sure this is working as expected now.