Skip to content

Commit

Permalink
ValueStreamBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
hoc081098 committed May 20, 2024
1 parent f65d5cd commit 9ccad74
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions packages/rxdart_flutter/lib/src/value_stream_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class ValueStreamBuilder<T> extends StatefulWidget {
class _ValueStreamBuilderState<T> extends State<ValueStreamBuilder<T>> {
late T currentData;
StreamSubscription<T>? subscription;
Object? error;

@override
void initState() {
Expand All @@ -125,18 +126,24 @@ class _ValueStreamBuilderState<T> extends State<ValueStreamBuilder<T>> {
}

@override
Widget build(BuildContext context) => widget._builder(context, currentData);
Widget build(BuildContext context) {
if (error != null) {
throw error!;
}
return widget._builder(context, currentData);
}

@pragma('vm:notify-debugger-on-exception')
void subscribe() {
final stream = widget._stream;

try {
currentData = stream.value;
error = null;
} on ValueStreamError catch (e, s) {
FlutterError.reportError(
FlutterErrorDetails(
exception: ValueStreamHasNoValueError(stream),
exception: error = ValueStreamHasNoValueError(stream),
stack: s,
library: 'rxdart_flutter',
),
Expand All @@ -145,7 +152,7 @@ class _ValueStreamBuilderState<T> extends State<ValueStreamBuilder<T>> {
} catch (e, s) {
FlutterError.reportError(
FlutterErrorDetails(
exception: e,
exception: error = e,
stack: s,
library: 'rxdart_flutter',
),
Expand All @@ -166,11 +173,12 @@ class _ValueStreamBuilderState<T> extends State<ValueStreamBuilder<T>> {
onError: (Object e, StackTrace s) {
FlutterError.reportError(
FlutterErrorDetails(
exception: UnhandledStreamError(e),
exception: error = UnhandledStreamError(e),
stack: s,
library: 'rxdart_flutter',
),
);
setState(_emptyFn);
},
);
}
Expand Down Expand Up @@ -200,7 +208,8 @@ class UnhandledStreamError extends Error {

@override
String toString() {
return '''Unhandled error from Stream: $error.
return '''Unhandled error from ValueStream: $error.
ValueStreamBuilder requires ValueStream never to emit error events.
You should use one of following methods to handle error before passing stream to ValueStreamBuilder:
* stream.handleError((e, s) { })
* stream.onErrorReturn(value)
Expand Down

0 comments on commit 9ccad74

Please sign in to comment.