A Dart try/catch block never runs even though the code throws
The catch block never runsShort answer
A try block only catches errors from a future if the future is awaited inside it. Without await, the try block only holds the future and has nothing to catch. Add await in an async function, and register PlatformDispatcher.instance.onError for what still escapes.
Fix it fast
1Find out which cause you have
Turn on the two Dart lints that flag futures nobody waits for. Every warning they print is a place where a catch cannot work.
# analysis_options.yaml linter: rules: - unawaited_futures - discarded_futures2Match what you see, then jump to its fix
- 1
unawaited_futures warns on the line, or the printed value is Instance of '_Future<...>'The future is not awaited inside the try blockFix - 2
The error shows in the console as unhandled, and it is not reported by FlutterError.onErrorThe error is raised outside any Flutter callback and no try block wraps itFix
- 1
What does the error look like?
The catch block never runsInstance of '_Future<String>' is printed instead of the value
Cause 1 of 2
The future is not awaited inside the try block
1Check
A failed async operation completes its future with an error. Dart's guide says to await the future inside the try block so that the error is caught there; without await you only hold the future.
dart analyze2Fix
Make the function async and put await in front of the call inside the try block.
Future<void> load() async {
try {
await api.fetchOrders(); // await keeps the error inside try
} catch (e, st) {
debugPrint('failed: $e');
}
}3Verify
Make the call fail on purpose, for example with no network, and watch the catch block run.
flutter runConfirmed by dart.dev
Cause 2 of 2
The error is raised outside any Flutter callback and no try block wraps it
1Check
Flutter documents that errors with no Flutter callback on the stack skip FlutterError.onError and go to PlatformDispatcher. Its own example is an async onPressed calling a plugin.
grep -rn "PlatformDispatcher\|FlutterError.onError" lib2Fix
Register the dispatcher hook in main() so these errors are handled, logged or sent to your crash service. Return true to mark an error as handled.
void main() {
PlatformDispatcher.instance.onError = (error, stack) {
debugPrint('uncaught: $error');
return true;
};
runApp(const MyApp());
}3Verify
Trigger the failing call and see the message from your handler instead of an unhandled exception.
flutter runConfirmed by docs.flutter.dev
How do I stop it coming back?
- Keep unawaited_futures and discarded_futures enabled, and treat their warnings as bugs.
- Every function that does async work and can fail should return Future and be awaited by its caller.
- Install the global error hooks early in main(), so nothing escapes silently in release builds.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
AudioCache has no play method after updating audioplayers
The method 'play' isn't defined for the type 'AudioCache'.
Since audioplayers 1.0, AudioCache only turns asset files into local files and caches them.
2 causes, each with a fixMobile apps
Flutter Bloc throws that emit was called after the event handler completed
emit was called after an event handler completed normally.
Each on<Event> handler gets its own Emitter, and it stops working the moment the handler's returned Future completes.
2 causes, each with a fixMobile apps
analysis_options.yaml: the URI it includes can't be found
include_file_not_found
The analyzer reports include_file_not_found when the file named after include: in analysis_options.yaml does not resolve.
2 causes, each with a fixMobile apps
Dart error: the body might complete normally and return null for a non-nullable type
body_might_complete_normally
Dart found a path through your function or callback that reaches the closing brace without a return.
3 causes, each with a fixMobile apps
Dart throws ConcurrentModificationError while looping over a list
ConcurrentModificationError
Dart throws this when a collection changes size while something iterates it.
2 causes, each with a fixMobile apps
Dart cannot turn a compact timestamp like 20201101153000 into a DateTime
FormatException: Invalid date format
DateTime.parse accepts a subset of ISO 8601, and a time part must be introduced by T or a space.
2 causes, each with a fix