Unverified Commit 47f12cae authored by Casey Rogers's avatar Casey Rogers Committed by GitHub

made top level if checks gaurd clauses (#135070)

This is a tiny tweak to replace some top level if clauses with guard clauses in `FutureBuilder`. I find the resultant code much more readable, but this is a matter of taste and I didn't see any info one way or another on it in the style guide so let me know if this is not to your all's preference.
parent 433bca5e
......@@ -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
......@@ -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);
}
}
......
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