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
除非另有说明,否则本网站上的文档反映的是 Dart 3.6.0。页面最后更新于 2025-02-05。 查看源代码 或 报告问题.