跳到主要内容

implicit_call_tearoffs

稳定
核心
修复可用

当将对象用作 Function 时,显式地对其 call 方法进行撕裂。

详情

#

在将对象赋值给 Function 类型时,显式地对其 .call 方法进行撕裂。显式撕裂减少了隐晦性。未来的语言版本可能会移除隐式的 call 撕裂。

不推荐

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 文件中,将 implicit_call_tearoffs 添加到 linter > rules

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