目录

avoid_dynamic_calls

Avoid method calls or property accesses on a dynamic target.

此规则自 Dart 2.12 版本起可用。

详情

#

DO avoid method calls or accessing properties on an object that is either explicitly or implicitly statically typed dynamic. Dynamic calls are treated slightly different in every runtime environment and compiler, but most production modes (and even some development modes) have both compile size and runtime performance penalties associated with dynamic calls.

Additionally, targets typed dynamic disables most static analysis, meaning it is easier to lead to a runtime NoSuchMethodError or TypeError than properly statically typed Dart code.

There is an exception to methods and properties that exist on Object?:

  • a.hashCode
  • a.runtimeType
  • a.noSuchMethod(someInvocation)
  • a.toString()

... these members are dynamically dispatched in the web-based runtimes, but not in the VM-based ones. Additionally, they are so common that it would be very punishing to disallow any.toString() or any == true, for example.

Note that despite Function being a type, the semantics are close to identical to dynamic, and calls to an object that is typed Function will also trigger this lint.

Dynamic calls are allowed on cast expressions (as dynamic or as Function).

BAD:

dart
void explicitDynamicType(dynamic object) {
  print(object.foo());
}

void implicitDynamicType(object) {
  print(object.foo());
}

abstract class SomeWrapper {
  T doSomething<T>();
}

void inferredDynamicType(SomeWrapper wrapper) {
  var object = wrapper.doSomething();
  print(object.foo());
}

void callDynamic(dynamic function) {
  function();
}

void functionType(Function function) {
  function();
}

GOOD:

dart
void explicitType(Fooable object) {
  object.foo();
}

void castedType(dynamic object) {
  (object as Fooable).foo();
}

abstract class SomeWrapper {
  T doSomething<T>();
}

void inferredType(SomeWrapper wrapper) {
  var object = wrapper.doSomething<Fooable>();
  object.foo();
}

void functionTypeWithParameters(Function() function) {
  function();
}

使用方法

#

要启用 avoid_dynamic_calls 规则,请在你的 analysis_options.yaml 文件中,在 linter > rules 下添加 avoid_dynamic_calls

analysis_options.yaml
yaml
linter:
  rules:
    - avoid_dynamic_calls