Skip to content

Can class variables explicitly checked for null be assumed non-null? #1472

Closed
@tarobins

Description

@tarobins

I testing the following class in dartpad using Dart SDK 2.12.0-51.0.dev

class Coffee {
  
  List<String>? types;
  
  Coffee(this.types);
  
  void printType() {
    if (types != null) {
      print(types[0]);
    }
  }
}

The compiler complains that types may be null in the print statement.

"An expression whose value can be 'null' must be null-checked before it can be dereferenced - line 16"

I realize the docs say

" The analyzer can’t model the flow of your whole application, so it can’t predict the values of global variables or class fields."

but I wonder in the case were we explicitly check if a class var is not null is supposed to be or could be handled?

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureProposed language feature that solves one or more problemsstate-duplicateThis issue or pull request already exists

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions