From 47f12cae1f3f96a37951c1ace0e4998fe304bec3 Mon Sep 17 00:00:00 2001 From: Casey Rogers Date: Wed, 27 Sep 2023 14:29:13 -0700 Subject: [PATCH] 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. --- packages/flutter/lib/src/widgets/async.dart | 67 +++++++++++---------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/packages/flutter/lib/src/widgets/async.dart b/packages/flutter/lib/src/widgets/async.dart index 36b0f77eb48e..b8aed1f94fc8 100644 --- a/packages/flutter/lib/src/widgets/async.dart +++ b/packages/flutter/lib/src/widgets/async.dart @@ -595,13 +595,14 @@ class _FutureBuilderState extends State> { @override void didUpdateWidget(FutureBuilder 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 extends State> { } void _subscribe() { - if (widget.future != null) { - final Object callbackIdentity = Object(); - _activeCallbackIdentity = callbackIdentity; - widget.future!.then((T data) { - if (_activeCallbackIdentity == callbackIdentity) { - setState(() { - _snapshot = AsyncSnapshot.withData(ConnectionState.done, data); - }); - } - }, onError: (Object error, StackTrace stackTrace) { - if (_activeCallbackIdentity == callbackIdentity) { - setState(() { - _snapshot = AsyncSnapshot.withError(ConnectionState.done, error, stackTrace); - }); - } - assert(() { - if (FutureBuilder.debugRethrowError) { - Future.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((T data) { + if (_activeCallbackIdentity == callbackIdentity) { + setState(() { + _snapshot = AsyncSnapshot.withData(ConnectionState.done, data); + }); } + }, onError: (Object error, StackTrace stackTrace) { + if (_activeCallbackIdentity == callbackIdentity) { + setState(() { + _snapshot = AsyncSnapshot.withError(ConnectionState.done, error, stackTrace); + }); + } + assert(() { + if (FutureBuilder.debugRethrowError) { + Future.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); } }