avoid_ returning_ this
Avoid returning this from methods just to enable a fluent interface.
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:
var buffer = StringBuffer()
.write('one')
.write('two')
.write('three');
GOOD:
var buffer = StringBuffer()
..write('one')
..write('two')
..write('three');
Enable
#
To enable the
avoid_returning_this
rule, add
avoid_returning_this
under
linter > rules
in your
analysis_options.yaml
file:
linter:
rules:
- avoid_returning_this
If you're instead using the YAML map syntax to configure linter rules,
add
avoid_returning_this: true
under
linter > rules:
linter:
rules:
avoid_returning_this: true
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Report an issue.