redirect_to_non_const_constructor
A constant redirecting constructor can't redirect to a non-constant constructor.
Description
#The analyzer produces this diagnostic when a constructor marked as const
redirects to a constructor that isn't marked as const
.
Example
#The following code produces this diagnostic because the constructor C.a
is marked as const
but redirects to the constructor C.b
, which isn't:
dart
class C {
const C.a() : this.b();
C.b();
}
Common fixes
#If the non-constant constructor can be marked as const
, then mark it as
const
:
dart
class C {
const C.a() : this.b();
const C.b();
}
If the non-constant constructor can't be marked as const
, then either
remove the redirect or remove const
from the redirecting constructor:
dart
class C {
C.a() : this.b();
C.b();
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。