目錄

避免_在函式字面值中_的forEach_呼叫

避免使用帶有函式字面值的 forEach

此規則自 Dart 2.0 起可用。

規則集:建議flutter

此規則有可用的快速修正

詳細資訊

#

避免使用帶有函式字面值的 forEach

for 迴圈讓開發人員可以清楚且明確地表達其意圖。for 迴圈主體中的 return 會從函式主體返回,而 forEach 閉包主體中的 return 只會為 forEach 的該次迭代傳回值。for 迴圈的主體可以包含 await,而 forEach 的閉包主體則不能。

不良示範

dart
people.forEach((person) {
  ...
});

良好示範

dart
for (var person in people) {
  ...
}

用法

#

若要啟用 avoid_function_literals_in_foreach_calls 規則,請在 analysis_options.yaml 檔案中的 linter > rules 下方新增 avoid_function_literals_in_foreach_calls

analysis_options.yaml
yaml
linter:
  rules:
    - avoid_function_literals_in_foreach_calls