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:
dart
class C {
C(int x) : assert(x > 0), this.name();
C.name() {}
}
Common fixes
#If the assert isn't needed, then remove it:
dart
class C {
C(int x) : this.name();
C.name() {}
}
If the assert is needed, then convert the constructor into a factory constructor:
dart
class C {
factory C(int x) {
assert(x > 0);
return C.name();
}
C.name() {}
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。