avoid_returning_this
Avoid returning this from methods just to enable a fluent interface.
This rule is available as of Dart 2.0.
Details
#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');
Usage
#To enable the avoid_returning_this
rule,
add avoid_returning_this
under linter > rules in your
analysis_options.yaml
file:
analysis_options.yaml
yaml
linter:
rules:
- avoid_returning_this
除非另有说明,文档之所提及适用于 Dart 3.5.3 版本,本页面最后更新时间: 2024-08-02。 查看文档源码 或者 报告页面问题。