-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
avoid_future_catcherror
Description
Avoid calling Future.catchError.
Details
Calling the catchError method on a Future can lead to type errors at runtime if the
runtime type of the Future is more specific than that of the variable or argument that references it.
Kind
Does this enforce style advice? Guard against errors? Other?
Bad Examples
void doSomething() {
unawaited(doSomethingAsync().catchError((_) => null));
}
Future<Object?> doSomethingAsync() => Future<Object?>.value(1);Good Examples
void doSomething() {
unawaited(doSomethingAsync().then(
(Object? obj) => obj,
onError: (_) => null,
));
}
Future<Object?> doSomethingAsync() async => Future<Object?>.value(1);Discussion
This is the SDK issue indicating tracking that statically correct usage of Future.catchError can lead to a runtime ArgumentError: #51248. This investigation was initiated by flutter/flutter#114031, which was a runtime crasher in the Flutter CLI tool that was very difficult to track down.
I intend to implement this lint in the Flutter CLI tool, and will discuss enabling it across the Flutter SDK.
The "Bad example" above will not hit this runtime issue. Should I have included an example that would crash at runtime? A trivial example of this would be:
void doSomething() {
unawaited(doSomethingAsync().catchError((_) => 'hi'));
}
Future<Object?> doSomethingAsync() => Future<bool>.error(true);Discussion checklist
- List any existing rules this proposal modifies, complements, overlaps or conflicts with.
- List any relevant issues (reported here, the SDK Tracker, or elsewhere).
- If there's any prior art (e.g., in other linters), please add references here.
- If this proposal corresponds to Effective Dart or Flutter Style Guide advice, please call it out. (If there isn't any corresponding advice, should there be?)
- If this proposal is motivated by real-world examples, please provide as many details as you can. Demonstrating potential impact is especially valuable.