dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.23k stars 1.57k forks source link

Dart Analyzer can't seem to figure out whether a List child is const or new #33369

Closed maryx closed 6 years ago

maryx commented 6 years ago

Is this expected?

matanlurey commented 6 years ago

@maryx:

It seems like the Dart Analyzer can't determine whether the child of a <Widget>[] is new or const, so we have to manually specify new. E.g. for this code:

This feature was reverted. There is no such thing as "automatic const" anymore, only "optional new". Once you specify the context as const once, then the remaining constructor calls are implicitly const. Consider:

class Animal {
  const Animal();
}

void main() {
  // Same as "final animals = [new Animal()]"
  final animals = [Animal()];

  // Same as "const animals = const [const Animal()]"
  const animals = [Animal()]
}

In your case children: is not a const context.


re: prefer_const_literals_to_create_immutables

You might want to also file this as dart-lang/linter, as it isn't part of the SDK proper: https://github.com/dart-lang/linter/issues

maryx commented 6 years ago

Ah thanks, I hadn't been sure where to file this - redirecting and closing this.