跳到主要內容

undefined_constructor_in_initializer

類 '{0}' 沒有名為 '{1}' 的建構函式。

類 '{0}' 沒有未命名的建構函式。

描述

#

當在建構函式的初始化列表中呼叫超類建構函式,但超類未定義該建構函式時,分析器會生成此診斷資訊。

示例

#

以下程式碼會生成此診斷資訊,因為 A 沒有未命名的建構函式

dart
class A {
  A.n();
}
class B extends A {
  B() : super();
}

以下程式碼會生成此診斷資訊,因為 A 沒有名為 m 的建構函式

dart
class A {
  A.n();
}
class B extends A {
  B() : super.m();
}

常見修復方法

#

如果超類定義了應該呼叫的建構函式,則更改要呼叫的建構函式

dart
class A {
  A.n();
}
class B extends A {
  B() : super.n();
}

如果超類沒有定義合適的建構函式,則定義要呼叫的建構函式

dart
class A {
  A.m();
  A.n();
}
class B extends A {
  B() : super.m();
}