Allow const constructor with mixins that have getter-only fields

Originally posted at google/built_value.dart#694

I’m getting const_constructor_with_mixin_with_field when using this mixin:

mixin RouteEnum on EnumClass {
  String get title;

  String get subtitle;

  IconData get icon;

  Color get color;

I have other code in the mixin that depends on those fields, which are defined in the subclass.

Since I’m not actually defining any fields in the mixin itself, it seems like this error shouldn’t necessarily happen.

Shouldn’t getter-only fields be treated similarly to functions?

Edit: Forgot to add my use case for this…
I’m using Flutter with a BottomNavigationBar and separate navigation within each tab.

As a result, I’d like to keep all of this related information with the EnumClass because it makes more sense structurally to me than to have functions within the various Widgets.

Author: Fantashit

1 thought on “Allow const constructor with mixins that have getter-only fields

  1. It shouldn’t happen.
    The mixin class does not declare any instance variable. I doesn’t actually declare any concrete anything, it just adds members to its interface.

    This is an implementation bug. I’ll move it to the SDK repository.

    Simple reproduction:

    class C {
      const C();
    mixin M {
      int get foo;DART_CONFIGURATION=ReleaseX64 .../sdk/bin/dartanalyzer_developer --dart-sdk=/usr/local/google/home/lrn/dart/co/sdk/sdk cmf.dart
    class S extends C with M {
      const S();
      int get foo => 42;
    main() {
     const S().foo;

    gives the following error from the analyzer:

    Analyzing cmf.dart...
      error • Const constructor can't be declared for a class with a mixin that declares an instance field at cmf.dart:8:9 • const_constructor_with_mixin_with_field
    1 error found.

    It should not. The common front-end does not give any error.DART_CONFIGURATION=ReleaseX64 …/sdk/bin/dartanalyzer_developer –dart-sdk=/usr/local/google/home/lrn/dart/co/sdk/sdk cmf.dart

Comments are closed.