Skip to content

Dart cannot be sure that the method argument is not null if the argument is used in a closure #1597

Open
@vadlit

Description

@vadlit

By some reason Dart cannot be sure that the method argument is not null if the argument is used in a closure. Property 'isEven' cannot be accessed on 'int?' because it is potentially null

void main() {
  foo(1);
}

void foo(int? nullable) {
  Future.delayed(Duration(seconds:0)).then((_)
  {
    if (nullable == null) { 
      nullable = 5;
    }
    if (nullable == null) {
      return;
    }
    print(nullable.isEven);
  });
}

In this case, as far as I undestand, there's no chance that the 'nullable' argument suddenly becomes nullable after the 1st check, because it's just a method argument, not class field.
The problem is not reproduced if remove the first check.

Dart 2.12.2
Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    requestRequests to resolve a particular developer problem

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions