avoid_function_literals_in_foreach_calls
避免搭配函式實字使用 forEach
。
詳細資訊
#請「避免」搭配函式實字使用 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
如果您改用 YAML map 語法來設定 linter 規則,請在 linter > rules 下新增 avoid_function_literals_in_foreach_calls: true
analysis_options.yaml
yaml
linter:
rules:
avoid_function_literals_in_foreach_calls: true
除非另有說明,否則本網站上的文件反映的是 Dart 3.7.1 版本。頁面最後更新於 2025-03-07。檢視原始碼 或 回報問題。