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:
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
:
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:
class C {
C.a() : this.b();
C.b();
}
Unless stated otherwise, the documentation on this site reflects Dart 3.7.3. Page last updated on 2025-05-08. View source or report an issue.