避免_在_foreach_呼叫中_使用_函式_字面量
避免在 forEach 中使用函式字面量。
詳情
#避免在 forEach 中使用函式字面量。
for 迴圈讓開發者能夠清晰明確地表達其意圖。for 迴圈體內的 return 語句會從整個函式體返回,而 forEach 閉包體內的 return 只會返回當前迭代的值。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如果您使用的是 YAML 對映語法配置 linter 規則,請在 linter > rules 下新增 avoid_function_literals_in_foreach_calls: true
analysis_options.yaml
yaml
linter:
rules:
avoid_function_literals_in_foreach_calls: true