Skip to content

Addressing user expectations around null-aware elements #61167

@chloestefantsova

Description

@chloestefantsova

Consider the following program.

class A {
  int? get x {
    print("Getter 'x' called.");
    return 0;
  }

  foo() {
    return [0, if (x != null) x!, 1];
  }
}

main() {
  new A().foo();
}

In that program, replacing if (x != null) x! with ?x changes the observable behavior. In the former case it will print the line Getter 'x' called. twice, and in the latter — once. This is the reason behind the linter not offering such a rewrite when getters are tested for being null.

However, as hinted by #61122, it might go against the intuition of the programmer, since in practice many getters are stable and don't have any side effects.

Should our tooling account for such user expectations and possibly provide a hint or address it in any other way?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.feature-null-aware-elementsImplementation of the Null-aware elements featuretype-questionA question about expected behavior or functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions