dart-code-metrics icon indicating copy to clipboard operation
dart-code-metrics copied to clipboard

[New rule] Warn when returning Future<void> instead of Void

Open vHanda opened this issue 3 years ago • 0 comments

Please describe what the rule should do: Should give me an error when passing an async void function in place of a sync void function.

If your rule is inspired by other please provide link to it:

  • https://stackoverflow.com/questions/72529983/flutter-how-to-detect-async-code-in-a-ui-callback/

What category of rule is this? (place an "X" next to just one item)

[x ] Warns about a potential error (problem) [ ] Suggests an alternate way of doing something (suggestion) [ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about (it will be better if you can provide both good and bad examples):

typedef VoidCallback = void Function();

void hello(VoidCallback foo) {
  foo();
  print("hello dodo");
}

Future<void> cb() async {
  await Future.delayed(Duration(seconds: 1));
  print('Callback');
}

void main() {
  // Lets get an error when passing the return type 
  // doesn't match exactly
  hello(cb);
}
RaisedButton(
  child: Text("Next Page"),
  onPressed: () async {
    // This could be any long running task
    // The user can now press this button multiple times
    // and each subsequent press will result in an error
    // as the context won't be valid any more.
    await Future.delayed(Duration(seconds: 1));
    Navigator.pop(context);
  }),
),

Are you willing to submit a pull request to implement this rule? Maybe. But I don't want to commit to anything.

vHanda avatar Jun 08 '22 15:06 vHanda

Available in 4.18.0 🚀

incendial avatar Sep 12 '22 15:09 incendial