Skip to content

Final fields null checks aren't handled properly #59586

@Hari-07

Description

@Hari-07
class Person {
  final String? name;
  Person([this.name]);

  String printName() {
    if (name != null) return name!;
    return 'User';
  }
}

void main() {
  final a = Person();
  a.printName();
}

Consider this code for example. When the class is initialized since name is final, it can be determined statically that for this instance of the object the field name can't be changed anymore since its a final.

So then in the printName method, why does the analyzer require me to assert its non null

Metadata

Metadata

Assignees

No one assigned

    Labels

    closed-as-intendedClosed as the reported issue is expected behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions