跳到主內容

nullable_type_in_catch_clause

潛在的可空型別不能在 'on' 子句中使用,因為丟擲可空表示式是無效的。

描述

#

catch 子句中 on 後面的型別是可空型別時,分析器會產生此診斷。指定可空型別是無效的,因為無法捕獲 null(因為丟擲 null 是一個執行時錯誤)。

示例

#

以下程式碼會產生此診斷,因為當無法丟擲 null 時,異常型別被指定為允許 null

dart
void f() {
  try {
    // ...
  } on FormatException? {
  }
}

常見修復方法

#

從型別中移除問號

dart
void f() {
  try {
    // ...
  } on FormatException {
  }
}