how to restore FlutterError.onError?

863

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 have not 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.

Share:
863
MeanMan
Author by

MeanMan

Exploring the latest web frameworks, currently learning "MEAN" framework.

Updated on December 29, 2022

Comments

  • MeanMan
    MeanMan over 1 year

    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,