目录

avoid_returning_this

Avoid returning this from methods just to enable a fluent interface.

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

详情

#

From Effective Dart:

AVOID returning this from methods just to enable a fluent interface.

Returning this from a method is redundant; Dart has a cascade operator which allows method chaining universally.

Returning this is allowed for:

  • operators
  • methods with a return type different of the current class
  • methods defined in parent classes / mixins or interfaces
  • methods defined in extensions

BAD:

dart
var buffer = StringBuffer()
  .write('one')
  .write('two')
  .write('three');

GOOD:

dart
var buffer = StringBuffer()
  ..write('one')
  ..write('two')
  ..write('three');

使用方法

#

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

analysis_options.yaml
yaml
linter:
  rules:
    - avoid_returning_this