Skip to content

Make MyEnum.aValue.anInstanceVariable constant #2780

@polarby

Description

@polarby

I have noticed that dart won't accept the argument of enums as const value, although both enums and their arguments are exclusively const.
This really sucks when trying to keep the class constant, but just by accessing the definitely constant argument the class won't be accepted as a constant one.

enum Foo {
  bar('The answer to life, the universe, and everything.'),
  baz('Flutter and dart are awesome.');

  const Foo(this.message);

  final String message;
}

class Bar {
  final Foo foo;

  const Bar(this.foo)
      : assert(foo == Foo.bar),
        assert(foo.message == "Only real messages"); // Will not accept message as const
}
Screenshot in Android studio

Image 2023-1-18 at 9 42 PM

I am using:

  • Dart SDK version: 2.19.0-444.4.beta
  • Android Studio on MacOSX

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhanced-constRequests or proposals about enhanced constant expressionssmall-featureA small feature which is relatively cheap to implement.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions