how to restore FlutterError.onError?

6.3k Views Asked by At

In my app, I record the flutter onError to crashalytics,

FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterError;

While running the integration test, if some exceptions happens I get the below statement in the console and the test just hangs,

The following exception was thrown running a test: I/flutter (30479): A test overrode FlutterError.onError but either failed to return it to its original state, or had unexpected additional errors that it could not handle. Typically, this is caused by using expect() before restoring FlutterError.onError.

The above message in console suggests something is wrong with the onError overriding, how do I return FlutterError.onError to its original state as per the recommendation coming up in console.

Please note that I am using the newly recommended way for integration test,

5

There are 5 best solutions below

0
On

hmm, Ok.. it seems that we have a couple a issues related to that : link-1, link-2, link-3

Long story short: The integration_test lib seems to be optimized only for "happy paths" for now.

In my case, Firebase was conflicting with integration_test when this line was enabled:

   FirebaseMessaging.onBackgroundMessage(_onBackgroundOrTerminatedHandler);

The error was gone once I've disabled it (when running the integration tests).

0
On

Try to update to flutter 2.5.0. This seems to be fixed there.

It no longer gets stuck when running the test. So it continues to execute the rest of the tests. Although it still shows the _pendingExceptionDetails != null error

7
On

onError is a public static member of FlutterError, so technically it can be overridden by anyone from anywhere. The testWidgets() function itself overrides the FlutterError.onError with its own error handler as well before running the test body. You can read its source code for more info.

Basically, below is what happened:

testWidgets('', (tester) async { // onError is overridden with the handler of the test framework
  await app.main(); // onError is overridden again with crashlytics error handler
  //...
  expect(); // Flutter yells that you should not have touched its onError
});

The point is the Flutter test framework needs its onError to work properly. So whatever you do, remember to call the error handler of the test framework.

Below is the way I used in my project to "restore" the FlutterError.onError (and do something else):

testWidgets('', (tester) async {
  final originalOnError = FlutterError.onError!;
  FlutterError.onError = (FlutterErrorDetails details) {
    // do something like ignoring an exception
    originalOnError(details); // call test framework's error handler
  };
  // ...
  expect();
});

With some modification, I think your problem is solvable.

0
On

I was seeing this error because I forgot to call await tester.pumpAndSettle() after asking the test to tap a button.

This prevented the app from moving to the next screen. When I tried to find a specific TextField, it wasn't visible to the integration test.

The cause of the error in my case had nothing to do with actually needing to restor FlutterError.onError.

2
On

This helper function I wrote works for me:


Future<void> restoreFlutterError(Future<void> Function() call) async {
  final originalOnError = FlutterError.onError!;
  await call();
  final overriddenOnError = FlutterError.onError!;

  // restore FlutterError.onError
  FlutterError.onError = (FlutterErrorDetails details) {
    if (overriddenOnError != originalOnError) overriddenOnError(details);
    originalOnError(details);
  };
}

void main(){
  testWidgets("some test", (tester) async {
    await restoreFlutterError(() async {
      app.main();
      await tester.pumpAndSettle();
    });
    // ...
    expect(...);
  });
}

Anything that overrides FlutterError.onError can be wrapped around restoreFlutterError - the function makes sure that both onError handler (your and the one set by the test framework) are called.