Skip to content

Analyzer: phantom analyze issue for enums #49097

Description

@khobotin-elopage

The issue appeared after upgrading to Dart 2.17 & Flutter 3

STR:
Create new flutter project and write:

enum MyEnum {
  myValue(type: TextInputType.none);

  const MyEnum({this.type = TextInputType.text});

  final TextInputType type;
}

the code {this.type = TextInputType.text} throws an error The default value of an optional parameter must be constant.

But TextInputType.text is a constant value

static const TextInputType text = TextInputType._(0);

if I run dart analyze I will get

error • lib/main.dart:120:29 • The default value of an optional parameter must be constant. • non_constant_default_value


The most strange part starts when I go to TextInputType.text definition and then back, or just change a file (delete symbol in text and then undo action), the error dissapears. And now dart analyze show

No issues found!

But once I run command Restart Analysis Server, it go back.

It only applicable for enums. This code

class ClassA {
  const ClassA({this.type = TextInputType.text});

  final TextInputType type;
}

doesn't show any errors.

Another strange thing is: if I put together enum and class and set the same TextInputType default value, enum will not show the error (even after analyzer restart). Like this:

enum MyEnum {
  myValue(type: TextInputType.none);

  const MyEnum({this.type = TextInputType.text});

  final TextInputType type;
}

class ClassA {
  const ClassA({this.type = TextInputType.text});

  final TextInputType type;
}

But once I set TextInputType default value for enum other than text, and restart analyzer, error goes back.

Dart SDK version: 2.17.0 (stable) on macos_arm64
Flutter 3.0.1 • channel stable
IDE: VS Code

Metadata

Metadata

Assignees

Labels

P1A high priority bug; for example, a single project is unusable or has many test failureslegacy-area-analyzerUse area-devexp instead.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions