Skip to content

Commit

Permalink
made top level if checks gaurd clauses
Browse files Browse the repository at this point in the history
  • Loading branch information
caseycrogers authored and goderbauer committed Sep 26, 2023
1 parent d81c8aa commit d72cbdc
Showing 1 changed file with 35 additions and 32 deletions.
67 changes: 35 additions & 32 deletions packages/flutter/lib/src/widgets/async.dart
Original file line number Diff line number Diff line change
Expand Up @@ -595,13 +595,14 @@ class _FutureBuilderState<T> extends State<FutureBuilder<T>> {
@override
void didUpdateWidget(FutureBuilder<T> oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.future != widget.future) {
if (_activeCallbackIdentity != null) {
_unsubscribe();
_snapshot = _snapshot.inState(ConnectionState.none);
}
_subscribe();
if (oldWidget.future == widget.future) {
return;
}
if (_activeCallbackIdentity != null) {
_unsubscribe();
_snapshot = _snapshot.inState(ConnectionState.none);
}
_subscribe();
}

@override
Expand All @@ -614,33 +615,35 @@ class _FutureBuilderState<T> extends State<FutureBuilder<T>> {
}

void _subscribe() {
if (widget.future != null) {
final Object callbackIdentity = Object();
_activeCallbackIdentity = callbackIdentity;
widget.future!.then<void>((T data) {
if (_activeCallbackIdentity == callbackIdentity) {
setState(() {
_snapshot = AsyncSnapshot<T>.withData(ConnectionState.done, data);
});
}
}, onError: (Object error, StackTrace stackTrace) {
if (_activeCallbackIdentity == callbackIdentity) {
setState(() {
_snapshot = AsyncSnapshot<T>.withError(ConnectionState.done, error, stackTrace);
});
}
assert(() {
if (FutureBuilder.debugRethrowError) {
Future<Object>.error(error, stackTrace);
}
return true;
}());
});
// An implementation like `SynchronousFuture` may have already called the
// .then closure. Do not overwrite it in that case.
if (_snapshot.connectionState != ConnectionState.done) {
_snapshot = _snapshot.inState(ConnectionState.waiting);
if (widget.future == null) {
// There is no future to subscribe to, do nothing.
return;
}
final Object callbackIdentity = Object();
_activeCallbackIdentity = callbackIdentity;
widget.future!.then<void>((T data) {
if (_activeCallbackIdentity == callbackIdentity) {
setState(() {
_snapshot = AsyncSnapshot<T>.withData(ConnectionState.done, data);
});
}
}, onError: (Object error, StackTrace stackTrace) {
if (_activeCallbackIdentity == callbackIdentity) {
setState(() {
_snapshot = AsyncSnapshot<T>.withError(ConnectionState.done, error, stackTrace);
});
}
assert(() {
if (FutureBuilder.debugRethrowError) {
Future<Object>.error(error, stackTrace);
}
return true;
}());
});
// An implementation like `SynchronousFuture` may have already called the
// .then closure. Do not overwrite it in that case.
if (_snapshot.connectionState != ConnectionState.done) {
_snapshot = _snapshot.inState(ConnectionState.waiting);
}
}

Expand Down

0 comments on commit d72cbdc

Please sign in to comment.