跳到主要內容

switch_on_type

避免對“Type”使用 switch 語句。

描述

#

當對 Type 的值或 TypetoString 呼叫使用 switch 語句或 switch 表示式時,分析器會生成此診斷資訊。

示例

#

以下程式碼會生成此診斷資訊,因為 switch 語句用於 Type

dart
void f(Object o) {
  switch (o.runtimeType) {
    case const (int):
      print('int');
    case const (String):
      print('String');
  }
}

常見修復

#

請改用對變數進行模式匹配

dart
void f(Object o) {
  switch (o) {
    case int():
      print('int');
    case String():
      print('String');
  }
}