takeException method Null safety

dynamic takeException()

Returns the exception most recently caught by the Flutter framework.

Call this if you expect an exception during a test. If an exception is thrown and this is not called, then the exception is rethrown when the testWidgets call completes.

If two exceptions are thrown in a row without the first one being acknowledged with a call to this method, then when the second exception is thrown, they are both dumped to the console and then the second is rethrown from the exception handler. This will likely result in the framework entering a highly unstable state and everything collapsing.

It's safe to call this when there's no pending exception; it will return null in that case.

Implementation

dynamic takeException() {
  assert(inTest);
  final dynamic result = _pendingExceptionDetails?.exception;
  _pendingExceptionDetails = null;
  return result;
}