Skip to content

"Create function '...'" is missing ? for nullable variables when invoked in a class method #48086

Closed

Description

@DanTup
class A {
  void f() {
    // inside a class method
    String? d = '';
    myUndefinedMethod(0, bbb: 1.0, ccc: '2', ddd: d);
  }
}

// at the top level
String? d = '';
final a = myUndefinedMethod(0, bbb: 1.0, ccc: '2', ddd: d);

Using the "create function" quick-fix on both myUndefinedMethods produces different results:

void myUndefinedMethod(int i, {double bbb, String ccc, String ddd}) {} // missing ? on ddd
void myUndefinedMethod(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'm not sure where this gets populated though.

Activity

  1. DanTup commented on Jan 6, 2022

    @DanTup
    CollaboratorAuthor

    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):

    Screenshot 2022-01-06 at 11 36 50

    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).

  2. DanTup commented on Jan 6, 2022

    @DanTup
    CollaboratorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions