Skip to content

Commit 073be58

Browse files
YeungKCvictorsanni
andauthored
Delegate createStream in ResizeImage and ScrollAwareImageProvider (flutter#194756)
`ImageProvider.resolve` calls `createStream` on the provider that receives the call. `ScrollAwareImageProvider` and `ResizeImage` wrap a provider, but they do not override `createStream`. Thus they return a plain `ImageStream`, and the `createStream` override of the wrapped provider is never called. The `Image` widget always wraps its provider in `ScrollAwareImageProvider`. `cacheWidth` and `cacheHeight` wrap it in `ResizeImage`. Thus a custom `ImageStream` subclass from a custom provider is silently dropped. The issue gives an example: a stream that cancels a network request when the `Image` widget is disposed. This change adds a `createStream` override to the two wrappers. Each override returns `imageProvider.createStream(configuration)`. There is no API change. The change has an effect only on providers that override `createStream`. In the issue, a maintainer said that this is ["just an oversight in the implementation"](flutter#107000 (comment)) and that both wrappers ["should be updated to override missing virtual overrides to delegate to the wrapped provider"](flutter#107000 (comment)). In `ScrollAwareImageProvider`, `createStream` is the only method of the resolve path that is not delegated. `ResizeImage` keeps its own `obtainKey` and the inherited `resolveStreamForKey` on purpose, because they use its own `ResizeImageKey`. Fixes flutter#107000 ## Tests - Added `CustomImageStream` and `CustomStreamImageProvider` to `packages/flutter/test/painting/image_test_utils.dart`. The key of `CustomStreamImageProvider` never resolves, so the provider does not load an image. - Added `ScrollAwareImageProvider delegates createStream to the wrapped provider` to `packages/flutter/test/widgets/scroll_aware_image_provider_test.dart`. - Added `ResizeImage delegates createStream to the wrapped provider` to `packages/flutter/test/painting/image_provider_resize_image_test.dart`. Without the fix, the two new tests fail. Each test gets a plain `ImageStream` and not a `CustomImageStream`. With the fix, the two tests pass. All of `test/painting`, `test/widgets/scroll_aware_image_provider_test.dart`, `test/widgets/image_test.dart` and `test/widgets/image_resolution_test.dart` pass locally. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant in-code documentation (doc comments with `///`). No doc change is necessary. - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/CONTRIBUTING.ai.md [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Victor Sanni <[email protected]>
1 parent 2082411 commit 073be58

5 files changed

Lines changed: 71 additions & 4 deletions

File tree

‎packages/flutter/lib/src/painting/image_provider.dart‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,10 @@ class ResizeImage extends ImageProvider<ResizeImageKey> {
13431343
return provider;
13441344
}
13451345

1346+
@override
1347+
ImageStream createStream(ImageConfiguration configuration) =>
1348+
imageProvider.createStream(configuration);
1349+
13461350
@override
13471351
@Deprecated(
13481352
'Implement loadImage for image loading. '

‎packages/flutter/lib/src/widgets/scroll_aware_image_provider.dart‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ class ScrollAwareImageProvider<T extends Object> extends ImageProvider<T> {
5151
/// The [context] object is the [BuildContext] of the [State] using this
5252
/// provider. It is used to determine scrolling velocity during [resolve].
5353
///
54-
/// The [imageProvider] is used to create a key and load the image. It must
55-
/// not be null, and is assumed to interact with the cache in the normal way
56-
/// that [ImageProvider.resolveStreamForKey] does.
54+
/// The [imageProvider] is used to create a stream, obtain a key, and load the
55+
/// image. It is assumed to interact with the cache in the normal way that
56+
/// [ImageProvider.resolveStreamForKey] does.
5757
const ScrollAwareImageProvider({required this.context, required this.imageProvider});
5858

5959
/// The context that may or may not be enclosed by a [Scrollable].
@@ -63,7 +63,9 @@ class ScrollAwareImageProvider<T extends Object> extends ImageProvider<T> {
6363
/// been resolved.
6464
final DisposableBuildContext context;
6565

66-
/// The wrapped image provider to delegate [obtainKey] and [loadImage] to.
66+
/// The wrapped image provider to delegate [createStream], [obtainKey],
67+
/// [loadImage], and (once this provider decides to load)
68+
/// [resolveStreamForKey] to.
6769
final ImageProvider<T> imageProvider;
6870

6971
@override
@@ -105,6 +107,10 @@ class ScrollAwareImageProvider<T extends Object> extends ImageProvider<T> {
105107
imageProvider.resolveStreamForKey(configuration, stream, key, handleError);
106108
}
107109

110+
@override
111+
ImageStream createStream(ImageConfiguration configuration) =>
112+
imageProvider.createStream(configuration);
113+
108114
@override
109115
ImageStreamCompleter loadBuffer(T key, DecoderBufferCallback decode) =>
110116
imageProvider.loadBuffer(key, decode);

‎packages/flutter/test/painting/image_provider_resize_image_test.dart‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:flutter_test/flutter_test.dart';
1111

1212
import '../image_data.dart';
1313
import '../rendering/rendering_tester.dart';
14+
import 'image_test_utils.dart';
1415

1516
void main() {
1617
TestRenderingFlutterBinding.ensureInitialized();
@@ -723,6 +724,21 @@ void main() {
723724
await completer.future;
724725
expect(isAsync, true);
725726
});
727+
728+
// Regression test for https://github.com/flutter/flutter/issues/107000
729+
test('delegates createStream to the wrapped provider', () {
730+
final resizeImage = ResizeImage(CustomStreamImageProvider(), width: 10);
731+
const configuration = ImageConfiguration(size: Size(20, 30), devicePixelRatio: 2.0);
732+
733+
expect(
734+
resizeImage.resolve(configuration),
735+
isA<CustomImageStream>().having(
736+
(CustomImageStream s) => s.configuration,
737+
'configuration',
738+
same(configuration),
739+
),
740+
);
741+
});
726742
});
727743
}
728744

‎packages/flutter/test/painting/image_test_utils.dart‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,22 @@ class TestImageProvider extends ImageProvider<TestImageProvider> {
5353
@override
5454
String toString() => '${describeIdentity(this)}()';
5555
}
56+
57+
/// An [ImageStream] subclass returned by [CustomStreamImageProvider.createStream].
58+
class CustomImageStream extends ImageStream {
59+
CustomImageStream(this.configuration);
60+
61+
/// The configuration passed to [CustomStreamImageProvider.createStream].
62+
final ImageConfiguration configuration;
63+
}
64+
65+
/// An image provider that returns a [CustomImageStream] from [createStream].
66+
///
67+
/// Its key never resolves, so it never loads.
68+
class CustomStreamImageProvider extends ImageProvider<Object> {
69+
@override
70+
Future<Object> obtainKey(ImageConfiguration configuration) => Completer<Object>().future;
71+
72+
@override
73+
ImageStream createStream(ImageConfiguration configuration) => CustomImageStream(configuration);
74+
}

‎packages/flutter/test/widgets/scroll_aware_image_provider_test.dart‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,28 @@ void main() {
569569
imageCache.maximumSize = oldSize;
570570
},
571571
);
572+
573+
// Regression test for https://github.com/flutter/flutter/issues/107000
574+
testWidgets('ScrollAwareImageProvider delegates createStream to the wrapped provider', (
575+
WidgetTester tester,
576+
) async {
577+
final DisposableBuildContext context = await createContext(tester);
578+
final imageProvider = ScrollAwareImageProvider<Object>(
579+
context: context,
580+
imageProvider: CustomStreamImageProvider(),
581+
);
582+
583+
const configuration = ImageConfiguration(size: Size(20, 30), devicePixelRatio: 2.0);
584+
585+
expect(
586+
imageProvider.resolve(configuration),
587+
isA<CustomImageStream>().having(
588+
(CustomImageStream s) => s.configuration,
589+
'configuration',
590+
same(configuration),
591+
),
592+
);
593+
});
572594
}
573595

574596
class TestWidget extends StatefulWidget {

0 commit comments

Comments
 (0)