one-member-abstracts

Same as one_member_abstracts in Dart lints

Path
lint/complexity/one-member-abstracts
Default
Off, opt in
Origin
Dart lints
Rule
304 of 512

Flags abstract classes whose only member is a single abstract method.

Such a class is just a function type with extra ceremony; a top-level function or a function-typed parameter does the same job. Classes that extend, mix in, or implement another type, or that declare any field, getter, or setter, are left alone because they carry more than one member.

abstract class Predicate { bool test(Object o); } // flagged
typedef Predicate = bool Function(Object o);      // ok

Invalid

example.dartdart
abstract class Predicate {
  bool test(Object value);
}

abstract class Loader {
  const Loader();
  Future<String> load(String path);
}

abstract interface class Formatter {
  String format(int value);
}

abstract class Adder {
  int operator +(int other);
}

abstract mixin class Runner {
  void run();
}
Make `test` a top-level function and remove this unnecessary abstract class.
1abstract class Predicate {
Make `load` a top-level function and remove this unnecessary abstract class.
4
5abstract class Loader {
Make `format` a top-level function and remove this unnecessary abstract class.
9
10abstract interface class Formatter {
Make `+` a top-level function and remove this unnecessary abstract class.
13
14abstract class Adder {
Make `run` a top-level function and remove this unnecessary abstract class.
17
18abstract mixin class Runner {

Valid

example.dartdart
class Concrete {
  void run() {}
}

abstract class WithField {
  int get id;
  void run();
}

abstract class TwoMethods {
  void start();
  void stop();
}

abstract class Base {}

abstract class Extending extends Base {
  void run();
}

abstract class Implementing implements Base {
  void run();
}

abstract class WithBody {
  void run() {}
}

abstract class WithState {
  int count = 0;
  void run();
}

abstract class WithSetter {
  set value(int v);
  void run();
}

abstract class Empty {}

Configure one-member-abstracts

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "complexity": {
        "one-member-abstracts": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/complexity/one-member-abstracts: <reason>