prefer_async_callback
v0.4.0 Warning Fix Type Annotations
Flags Future<void> Function() written where the AsyncCallback typedef from package:flutter/foundation.dart would say the same thing. The quick fix replaces the type and adds the import.
Only the exact shape is reported: no parameters, and a Future<void> return. Future<int> Function() and Future<void> Function(int) are left alone — there is no typedef for them.
A callback field on a widget, spelled out longhand:
class UploadButton { const UploadButton({required this.onPressed, this.onCancel});
final Future<void> Function() onPressed; final Future<void> Function()? onCancel;}The same type in a function signature and a return type:
void retryOnFailure(Future<void> Function() action) {}
Future<void> Function() saveDraftAction(String draftId) => () async {};And in a collection of pending work:
final List<Future<void> Function()> pendingUploads = [];import 'package:flutter/foundation.dart';
class UploadButton { const UploadButton({required this.onPressed, this.onCancel});
final AsyncCallback onPressed; final AsyncCallback? onCancel;}
void retryOnFailure(AsyncCallback action) {}
AsyncCallback saveDraftAction(String draftId) => () async {};
final List<AsyncCallback> pendingUploads = [];Shapes that are never reported
Section titled “Shapes that are never reported”AsyncCallback is exactly Future<void> Function(), so anything with a
parameter or a different return type has no typedef to swap in:
// A result to await, not just completion.Future<int> Function() fetchCount = () async => 0;
// Takes an argument.Future<void> Function(int index) removeAt = (_) async {};
// Synchronous — that one is `VoidCallback`; see prefer_void_callback.void Function() dismiss = () {};See also: AsyncCallback typedef
Configuration
Section titled “Configuration”This rule is in the opinionated preset, so it is on with
preset: opinionated, or by name:
rules: prefer_async_callback: trueTo turn it off again:
rules: prefer_async_callback: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”prefer_void_callback— Use ‘VoidCallback’ instead of ‘void Function()’.prefer_equatable_mixin— Prefer using EquatableMixin instead of extending Equatable.prefer_explicit_function_type— Prefer explicit function type annotations over the bare ‘Function’ type.prefer_explicit_parameter_names— Name the parameters of a function type.