Added rethrowError to FutureBuilder (#84308)

This commit is contained in:
Viren Khatri 2021-07-14 00:41:05 +05:30 committed by GitHub
parent d2057d50c4
commit 43ed3b6bed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 0 deletions

View File

@ -737,6 +737,14 @@ class FutureBuilder<T> extends StatefulWidget {
/// [AsyncSnapshot.hasError] will be true.)
final T? initialData;
/// Whether the latest error received by the asynchronous computation should
/// be rethrown or swallowed. This property is useful for debugging purposes.
///
/// When set to true, will rethrow the latest error only in debug mode.
///
/// Defaults to `false`, resulting in swallowing of errors.
static bool debugRethrowError = false;
@override
State<FutureBuilder<T>> createState() => _FutureBuilderState<T>();
}
@ -795,6 +803,13 @@ class _FutureBuilderState<T> extends State<FutureBuilder<T>> {
_snapshot = AsyncSnapshot<T>.withError(ConnectionState.done, error, stackTrace);
});
}
assert(() {
if(FutureBuilder.debugRethrowError) {
Future<Object>.error(error, stackTrace);
}
return true;
}());
});
_snapshot = _snapshot.inState(ConnectionState.waiting);
}

View File

@ -165,6 +165,24 @@ void main() {
));
expect(find.text('AsyncSnapshot<String>(ConnectionState.waiting, I, null, null)'), findsOneWidget);
});
testWidgets('debugRethrowError rethrows caught error', (WidgetTester tester) async {
FutureBuilder.debugRethrowError = true;
final Completer<void> caughtError = Completer<void>();
await runZonedGuarded(() async {
final Completer<String> completer = Completer<String>();
await tester.pumpWidget(FutureBuilder<String>(
future: completer.future,
builder: snapshotText,
), const Duration(seconds: 1));
completer.completeError('bad');
}, (Object error, StackTrace stack) {
expectSync(error, equals('bad'));
caughtError.complete();
});
await tester.pumpAndSettle();
expectSync(caughtError.isCompleted, isTrue);
FutureBuilder.debugRethrowError = false;
});
});
group('StreamBuilder', () {
testWidgets('gracefully handles transition from null stream', (WidgetTester tester) async {