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); } }