assert_in_redirecting_constructor
重定向建構函式不能有 'assert' 初始化器。
描述
#當重定向建構函式(重定向到同一個類中的另一個建構函式)在其初始化列表包含 assert 時,分析器會產生此診斷資訊。
示例
#以下程式碼會產生此診斷資訊,因為未命名建構函式是一個重定向建構函式,並且其初始化列表中也包含 assert
dart
class C {
C(int x) : assert(x > 0), this.name();
C.name() {}
}常見修復方法
#如果不需要 assert,則將其移除
dart
class C {
C(int x) : this.name();
C.name() {}
}如果需要 assert,則將建構函式轉換為工廠建構函式
dart
class C {
factory C(int x) {
assert(x > 0);
return C.name();
}
C.name() {}
}