Skip to content

Promote to non-null based on null-checking a conditional member access #1602

Closed
@leonsenft

Description

@leonsenft
  • Dart SDK 2.12.2
  • Reproducible in DartPad on Chrome

Consider the following snippet:

abstract class Identifier {  
  Uri? get moduleUri;
}

void f(Identifier? id) {
  if (id?.moduleUri != null) {
    print(id.moduleUri);
  }
}

Currently the id.moduleUrl within the body of the if statement produces an error:

line 7 • The property 'moduleUri' can't be unconditionally accessed because the receiver can be 'null'.
Try making the access conditional (using '?.') or adding a null check to the target ('!').

The expression id?.moduleUri != null can only be true if id is non-null. Could the compiler/analyzer perform this type flow analysis in order to promote id to a non-null type within the if statement?

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