Skip to main content

prefer-unwrapping-future-or

added in: 1.4.0
Pro+

Warns when a FutureOr is not unwrapped before being used.

Awaiting FutureOr without checking if the returned value is actually a Future results in unnecessary async gap.

Example

❌ Bad:

FutureOr<int> futureOrFunction() async => 1;

Future<void> main() async {
await futureOrFunction(); // LINT: Ensure that the type of this expression is 'Future' before awaiting it.
}

✅ Good:

FutureOr<int> futureOrFunction() async => 1;

Future<void> main() async {
final futureOr = futureOrFunction();

int result;
if (futureOr is Future) {
result = await futureOr;
} else {
result = futureOr;
}

// Or

final value = futureOr is Future ? await futureOr : futureOr;
}

Additional Resources