Skip to main content

one_member_abstracts

Learn about the one_member_abstracts linter rule.

Deprecated

Avoid defining a one-member abstract class when a simple function will do.

Details

#

NOTE: As one member abstracts are often a stepping stone, this lint rule has been deprecated as of Dart 3.13 and is set to be removed in a future release of the Dart SDK. Remove all inclusions of this lint from your analysis options. If you wish to keep enforcing it, consider implementing the rule with an analyzer plugin.

From Effective Dart:

AVOID defining a one-member abstract class when a simple function will do.

Unlike Java, Dart has first-class functions, closures, and a nice light syntax for using them. If all you need is something like a callback, just use a function. If you're defining a class and it only has a single abstract member with a meaningless name like call or invoke, there is a good chance you just want a function.

BAD:

dart
abstract class Predicate {
  bool test(item);
}

GOOD:

dart
typedef Predicate = bool Function(item);

Enable

#

To enable the one_member_abstracts rule, add one_member_abstracts under linter > rules in your analysis_options.yaml file:

analysis_options.yaml
yaml
linter:
  rules:
    - one_member_abstracts

If you're instead using the YAML map syntax to configure linter rules, add one_member_abstracts: true under linter > rules:

analysis_options.yaml
yaml
linter:
  rules:
    one_member_abstracts: true