Test a throwing error

Flutter tips Published on

Testing your app's error handling is super important. Sometimes, a widget might throw an error under certain conditions, and you need to make sure it happens correctly and that your app can handle it.

Why Test Errors?

Knowing that your code fails gracefully or throws expected errors helps you build more reliable apps. It confirms your error logic works as intended.

Catching Errors in Widget Tests

In Flutter widget tests, unhandled errors can make your test fail. To check if an error is thrown and what kind of error it is, you can temporarily change how Flutter handles errors during the test.

Here's how you can do it:

testWidgets('creating a widget with some param \u2192 throw', (WidgetTester tester) async {
  var exceptionRes;

  // Catch Flutter error or this will make your test fail
  FlutterError.onError = (details) {
    exceptionRes = details.exception;
  };

  await tester.pumpWidget(MyAppWithError);

  // Set Flutter onError back. Flutter will throw if not handled.
  FlutterError.onError = (details) \u2192 FlutterError.presentError(details);

  // Now you can check the caught error
  expect(exceptionRes, isNotNull);
  expect(exceptionRes, isInstanceOf<MyCustomException>());
});

Understanding the Code

  1. We declare a variable exceptionRes to store the error.
  2. We temporarily set FlutterError.onError to a function that catches the details.exception and saves it to exceptionRes.
  3. We run the test that should cause the widget to throw an error using tester.pumpWidget.
  4. We reset FlutterError.onError back to its default behavior (FlutterError.presentError) so other parts of your test or other tests aren't affected.
  5. We use expect to check if exceptionRes is not null (meaning an error was caught) and if it's the specific type of exception we expected (MyCustomException in this example).
Keep it Clean

Remember to always reset FlutterError.onError after your test is done checking the error. This prevents side effects in other tests.

This simple pattern lets you reliably test error-throwing situations in your Flutter widgets.

Save 3 months of work

Create your app using our 6 years of making Flutter apps and more than 50+ apps

kickstarter for flutter apps

Frequently Asked Questions

Why do I need to catch the error in the test?

In Flutter widget tests, uncaught errors usually cause the test to fail immediately. By temporarily setting `FlutterError.onError`, you prevent the test from failing right away and can instead examine the error that was thrown.

What does `tester.pumpWidget` do?

It builds the provided widget and triggers a frame, allowing the widget to render and potentially execute logic that might throw an error.

Should I always reset `FlutterError.onError`?

Yes, it's best practice to reset it after your test verifies the error. This ensures that the global error handling behavior is restored for subsequent tests and doesn't hide actual issues.

Read more
You may also be interested in
Published on
Preserve page scroll position  blog card image
Preserve page scroll position
Published on 2025-05-12T11:51:13.520Z
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved