assert_in_redirecting_constructor
A redirecting constructor can't have an 'assert' initializer.
Description
#The analyzer produces this diagnostic when a redirecting constructor (a constructor that redirects to another constructor in the same class) has an assert in the initializer list.
Example
#The following code produces this diagnostic because the unnamed constructor is a redirecting constructor and also has an assert in the initializer list:
class C {
C(int x) : assert(x > 0), this.name();
C.name() {}
}
Common fixes
#If the assert isn't needed, then remove it:
class C {
C(int x) : this.name();
C.name() {}
}
If the assert is needed, then convert the constructor into a factory constructor:
class C {
factory C(int x) {
assert(x > 0);
return C.name();
}
C.name() {}
}
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.