跳到主要內容

implicit_call_tearoffs

穩定
核心
可修正

當使用物件作為函式時,明確地 tear-off call 方法。

詳細資訊

#

務必 當指派給函式類型時,從物件明確地 tear off .call 方法。明確的 tear off 較少魔法。未來的語言版本可能會移除隱含的 call tear off。

不良範例

dart
class Callable {
  void call() {}
}
void callIt(void Function() f) {
  f();
}

callIt(Callable());

良好範例

dart
class Callable {
  void call() {}
}
void callIt(void Function() f) {
  f();
}

callIt(Callable().call);

啟用

#

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

analysis_options.yaml
yaml
linter:
  rules:
    - implicit_call_tearoffs

如果您改為使用 YAML map 語法來設定 linter 規則,請在 linter > rules 下新增 implicit_call_tearoffs: true

analysis_options.yaml
yaml
linter:
  rules:
    implicit_call_tearoffs: true