drainAndRethrowException function
Drains any pending exception from tester.takeException and rethrows it.
This surfaces stray async errors (e.g. unhandled MissingPluginException)
early, preventing them from silently corrupting test state — especially
important in PreviewTestBinding where unhandled errors can prematurely
complete the test.
Implementation
void drainAndRethrowException(WidgetTester tester) {
final pendingException = tester.takeException();
if (pendingException != null) {
if (pendingException is Error) throw pendingException;
if (pendingException is Exception) throw pendingException;
throw Exception(pendingException.toString());
}
}