From bcb2ac5a55b90b2ccfd50de4684c0cbb8aca9290 Mon Sep 17 00:00:00 2001 From: Dan Field Date: Thu, 1 Oct 2020 14:14:15 -0700 Subject: [PATCH] docs for image disposal (#67066) --- .../lib/src/painting/image_provider.dart | 4 ++-- .../flutter/lib/src/painting/image_stream.dart | 18 +++++++++++------- packages/flutter/lib/src/widgets/image.dart | 4 ++-- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/packages/flutter/lib/src/painting/image_provider.dart b/packages/flutter/lib/src/painting/image_provider.dart index b135784fcb..1c7b8bf257 100644 --- a/packages/flutter/lib/src/painting/image_provider.dart +++ b/packages/flutter/lib/src/painting/image_provider.dart @@ -284,7 +284,7 @@ typedef DecoderCallback = Future Function(Uint8List bytes, {int? cache /// void _updateImage(ImageInfo imageInfo, bool synchronousCall) { /// setState(() { /// // Trigger a build whenever the image changes. -/// _imageInfo?.image?.dispose(); +/// _imageInfo?.dispose(); /// _imageInfo = imageInfo; /// }); /// } @@ -292,7 +292,7 @@ typedef DecoderCallback = Future Function(Uint8List bytes, {int? cache /// @override /// void dispose() { /// _imageStream.removeListener(ImageStreamListener(_updateImage)); -/// _imageInfo?.image?.dispose(); +/// _imageInfo?.dispose(); /// _imageInfo = null; /// super.dispose(); /// } diff --git a/packages/flutter/lib/src/painting/image_stream.dart b/packages/flutter/lib/src/painting/image_stream.dart index 23a481a24c..069d683701 100644 --- a/packages/flutter/lib/src/painting/image_stream.dart +++ b/packages/flutter/lib/src/painting/image_stream.dart @@ -45,7 +45,7 @@ class ImageInfo { /// /// See also: /// - /// * [Image.clone] + /// * [Image.clone], which describes how and why to clone images. ImageInfo clone() { return ImageInfo( image: image.clone(), @@ -68,11 +68,15 @@ class ImageInfo { /// ```dart /// ImageInfo _imageInfo; /// set imageInfo (ImageInfo value) { - /// // If the image reference is exactly the same, or its a clone of the - /// // current reference, we can immediately dispose of it and avoid - /// // recalculating anything. - /// if (value == _imageInfo || value != null && _imageInfo != null && value.isCloneOf(_imageInfo)) { - /// value?.dispose(); + /// // If the image reference is exactly the same, do nothing. + /// if (value == _imageInfo) { + /// return; + /// } + /// // If it is a clone of the current reference, we must dispose of it and + /// // can do so immediately. Since the underlying image has not changed, + /// // We don't have any additional work to do here. + /// if (value != null && _imageInfo != null && value.isCloneOf(_imageInfo)) { + /// value.dispose(); /// return; /// } /// _imageInfo?.dispose(); @@ -467,7 +471,7 @@ abstract class ImageStreamCompleter with Diagnosticable { @visibleForTesting bool get hasListeners => _listeners.isNotEmpty; - /// We should avoid disposing a completer if it has never had a listener, even + /// We must avoid disposing a completer if it has never had a listener, even /// if all [keepAlive] handles get disposed. bool _hadAtLeastOneListener = false; diff --git a/packages/flutter/lib/src/widgets/image.dart b/packages/flutter/lib/src/widgets/image.dart index 068fcf634b..4a30bd3d31 100644 --- a/packages/flutter/lib/src/widgets/image.dart +++ b/packages/flutter/lib/src/widgets/image.dart @@ -1249,8 +1249,8 @@ class _ImageState extends State with WidgetsBindingObserver { /// /// If the listener from this state is the last listener on the stream, the /// stream will be disposed. To keep the stream alive, set `keepStreamAlive` - /// to true, which will add a passive listener on the stream and is compatible - /// with the [TickerMode] being off. + /// to true, which create [ImageStreamCompleterHandle] to keep the completer + /// alive and is compatible with the [TickerMode] being off. void _stopListeningToStream({bool keepStreamAlive = false}) { if (!_isListeningToStream) return;