Sdk: Allow const constructor with mixins that have getter-only fields

Created on 10 Aug 2019  Â·  4Comments  Â·  Source: dart-lang/sdk

Originally posted at https://github.com/google/built_value.dart/issues/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.

area-analyzer type-bug

Most helpful comment

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

All 4 comments

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

Any news?

Hi all,
when it available in Flutter?

Was this page helpful?
0 / 5 - 0 ratings