Unverified Commit 43ed3b6b authored by Viren Khatri's avatar Viren Khatri Committed by GitHub

Added rethrowError to FutureBuilder (#84308)

parent d2057d50
...@@ -737,6 +737,14 @@ class FutureBuilder<T> extends StatefulWidget { ...@@ -737,6 +737,14 @@ class FutureBuilder<T> extends StatefulWidget {
/// [AsyncSnapshot.hasError] will be true.) /// [AsyncSnapshot.hasError] will be true.)
final T? initialData; 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 @override
State<FutureBuilder<T>> createState() => _FutureBuilderState<T>(); State<FutureBuilder<T>> createState() => _FutureBuilderState<T>();
} }
...@@ -795,6 +803,13 @@ class _FutureBuilderState<T> extends State<FutureBuilder<T>> { ...@@ -795,6 +803,13 @@ class _FutureBuilderState<T> extends State<FutureBuilder<T>> {
_snapshot = AsyncSnapshot<T>.withError(ConnectionState.done, error, stackTrace); _snapshot = AsyncSnapshot<T>.withError(ConnectionState.done, error, stackTrace);
}); });
} }
assert(() {
if(FutureBuilder.debugRethrowError) {
Future<Object>.error(error, stackTrace);
}
return true;
}());
}); });
_snapshot = _snapshot.inState(ConnectionState.waiting); _snapshot = _snapshot.inState(ConnectionState.waiting);
} }
......
...@@ -165,6 +165,24 @@ void main() { ...@@ -165,6 +165,24 @@ void main() {
)); ));
expect(find.text('AsyncSnapshot<String>(ConnectionState.waiting, I, null, null)'), findsOneWidget); 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', () { group('StreamBuilder', () {
testWidgets('gracefully handles transition from null stream', (WidgetTester tester) async { testWidgets('gracefully handles transition from null stream', (WidgetTester tester) async {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment