跳到主要內容

rest_element_in_map_pattern

Map 模式不能包含 rest 模式。

描述

#

當 map 模式包含 rest 模式時,分析器會產生此診斷。Map 模式匹配的 map 可以包含比模式中顯式指定的鍵更多的鍵(只要指定的鍵匹配),因此 rest 模式是不必要的。

示例

#

以下程式碼產生此診斷,因為 map 模式包含 rest 模式

dart
void f(Map<int, String> x) {
  if (x case {0: _, ...}) {}
}

常見修復

#

移除 rest 模式

dart
void f(Map<int, String> x) {
  if (x case {0: _}) {}
}