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
The issue appeared after upgrading to Dart 2.17 & Flutter 3
STR:
Create new flutter project and write:
the code
{this.type = TextInputType.text}throws an errorThe default value of an optional parameter must be constant.But
TextInputType.textis a constant valueif I run
dart analyzeI will getThe most strange part starts when I go to TextInputType.text definition and then back, or just change a file (delete symbol in
textand then undo action), the error dissapears. And nowdart analyzeshowBut once I run command
Restart Analysis Server, it go back.It only applicable for enums. This code
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:
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