Rules / Style / prefer-async-callback

prefer-async-callback

lint/style/prefer-async-callback
recommendedflutter
Same as prefer_async_callback · Pyramid Lint

Flags the type Future<void> Function() in favor of Flutter's AsyncCallback.

Flutter's foundation library defines AsyncCallback as a typedef for exactly Future<void> Function(), the standard signature for an asynchronous, argument-less callback. Spelling the type out longhand is more verbose and less recognizable than the named alias that the rest of the framework uses. The rule matches only the zero-parameter function type returning Future<void>; any parameters or a different return type leave it untouched.

Invalid

example.dartdart
typedef OnDone = Future<void> Function();

class A {
  final Future<void> Function() onSave;

  A(this.onSave);

  void register(Future<void> Function() cb) {}
}

Future<void> Function() make() => () async {};

void f() {
  Future<void> Function() local = () async {};
  local();
}
Use AsyncCallback instead of Future<void> Function().
1typedef OnDone = Future<void> Function();
Use AsyncCallback instead of Future<void> Function().
3class A {
4 final Future<void> Function() onSave;
Use AsyncCallback instead of Future<void> Function().
7
8 void register(Future<void> Function() cb) {}
Use AsyncCallback instead of Future<void> Function().
10
11Future<void> Function() make() => () async {};
Use AsyncCallback instead of Future<void> Function().
13void f() {
14 Future<void> Function() local = () async {};

Valid

example.dartdart
typedef OnValue = void Function();
typedef OnData = Future<void> Function(int value);
typedef Getter = Future<int> Function();

class A {
  final void Function() onTap;
  final Future<void> Function(String) onData;

  A(this.onTap, this.onData);
}

Future<int> Function() make() => () async => 1;

void Function() other() => () {};

void f() {
  void Function() local = () {};
  local();
}

How to configure

Set the severity of prefer-async-callback in your falcon.json:

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "prefer-async-callback": "error"
      }
    }
  }
}