跳到主要内容

implicit_call_tearoffs

稳定
核心
可修复

当使用对象作为函数时,显式地 tear-off call 方法。

详情

#

应该 在将对象赋值给函数类型时,显式地 tear-off 对象的 .call 方法。显式 tear-off 的魔法更少。未来的语言版本可能会移除隐式调用 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 映射语法来配置 linter 规则,请在 linter > rules 下添加 implicit_call_tearoffs: true

analysis_options.yaml
yaml
linter:
  rules:
    implicit_call_tearoffs: true