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>> { ...@@ -595,13 +595,14 @@ class _FutureBuilderState<T> extends State<FutureBuilder<T>> {
@override @override
void didUpdateWidget(FutureBuilder<T> oldWidget) { void didUpdateWidget(FutureBuilder<T> oldWidget) {
super.didUpdateWidget(oldWidget); super.didUpdateWidget(oldWidget);
if (oldWidget.future != widget.future) { if (oldWidget.future == widget.future) {
if (_activeCallbackIdentity != null) { return;
_unsubscribe(); }
_snapshot = _snapshot.inState(ConnectionState.none); if (_activeCallbackIdentity != null) {
} _unsubscribe();
_subscribe(); _snapshot = _snapshot.inState(ConnectionState.none);
} }
_subscribe();
} }
@override @override
...@@ -614,33 +615,35 @@ class _FutureBuilderState<T> extends State<FutureBuilder<T>> { ...@@ -614,33 +615,35 @@ class _FutureBuilderState<T> extends State<FutureBuilder<T>> {
} }
void _subscribe() { void _subscribe() {
if (widget.future != null) { if (widget.future == null) {
final Object callbackIdentity = Object(); // There is no future to subscribe to, do nothing.
_activeCallbackIdentity = callbackIdentity; return;
widget.future!.then<void>((T data) { }
if (_activeCallbackIdentity == callbackIdentity) { final Object callbackIdentity = Object();
setState(() { _activeCallbackIdentity = callbackIdentity;
_snapshot = AsyncSnapshot<T>.withData(ConnectionState.done, data); widget.future!.then<void>((T data) {
}); if (_activeCallbackIdentity == callbackIdentity) {
} setState(() {
}, onError: (Object error, StackTrace stackTrace) { _snapshot = AsyncSnapshot<T>.withData(ConnectionState.done, data);
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);
} }
}, 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