メインコンテンツにスキップ

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');
  }
}