From d939d6b00a645cfeef237ae488f43bf01271e1f4 Mon Sep 17 00:00:00 2001 From: Rajesh Malviya Date: Mon, 20 Oct 2025 07:35:21 +0530 Subject: [PATCH] Fix Image.network not using cache when headers are specified (#176831) Fix a bug introduced by 97aae2ad3 (#171916), where `hashCode` is giving different results for values which `==` says are equal. Which broke caching for uses of `Image.network` widget when `headers` map was passed. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [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]. - [ ] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [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]. **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. [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [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 [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 --- .../lib/src/painting/_network_image_io.dart | 3 +- .../lib/src/painting/_network_image_web.dart | 8 ++- .../image_provider_network_image_test.dart | 53 +++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/painting/_network_image_io.dart b/packages/flutter/lib/src/painting/_network_image_io.dart index 68bed3a9927..f95d267337e 100644 --- a/packages/flutter/lib/src/painting/_network_image_io.dart +++ b/packages/flutter/lib/src/painting/_network_image_io.dart @@ -6,6 +6,7 @@ import 'dart:async'; import 'dart:io'; import 'dart:ui' as ui; +import 'package:collection/collection.dart'; import 'package:flutter/foundation.dart'; import 'binding.dart'; @@ -171,7 +172,7 @@ class NetworkImage extends image_provider.ImageProvider Object.hash(url, scale, headers); + int get hashCode => Object.hash(url, scale, const MapEquality().hash(headers)); @override String toString() => diff --git a/packages/flutter/lib/src/painting/_network_image_web.dart b/packages/flutter/lib/src/painting/_network_image_web.dart index 6eb02a70bd2..773968a1146 100644 --- a/packages/flutter/lib/src/painting/_network_image_web.dart +++ b/packages/flutter/lib/src/painting/_network_image_web.dart @@ -6,6 +6,7 @@ import 'dart:async'; import 'dart:js_interop'; import 'dart:ui' as ui; +import 'package:collection/collection.dart'; import 'package:flutter/foundation.dart'; import '../web.dart' as web; @@ -250,7 +251,12 @@ class NetworkImage extends image_provider.ImageProvider Object.hash(url, scale, webHtmlElementStrategy, headers); + int get hashCode => Object.hash( + url, + scale, + webHtmlElementStrategy, + const MapEquality().hash(headers), + ); @override String toString() => diff --git a/packages/flutter/test/painting/image_provider_network_image_test.dart b/packages/flutter/test/painting/image_provider_network_image_test.dart index 0aea42f2542..a23c0c66ba7 100644 --- a/packages/flutter/test/painting/image_provider_network_image_test.dart +++ b/packages/flutter/test/painting/image_provider_network_image_test.dart @@ -288,6 +288,51 @@ void main() { debugNetworkImageHttpClientProvider = null; }, skip: isBrowser); // [intended] Browser does not resolve images this way. + test('Network image with same arguments uses cache', () async { + final _FakeHttpClient mockHttpClient = _FakeHttpClient(); + debugNetworkImageHttpClientProvider = () => mockHttpClient; + mockHttpClient.request.response + ..statusCode = HttpStatus.ok + ..contentLength = kTransparentImage.length + ..content = [Uint8List.fromList(kTransparentImage)]; + + final Completer imageAvailable1 = Completer(); + final ImageProvider imageProvider1 = NetworkImage( + nonconst('testing.url'), + headers: nonconst({'key': 'value'}), + ); + expect(imageCache.liveImageCount, 0); + + final ImageStream result1 = imageProvider1.resolve(ImageConfiguration.empty); + result1.addListener( + ImageStreamListener((ImageInfo image, bool synchronousCall) { + imageAvailable1.complete(); + }), + ); + await imageAvailable1.future; + expect(imageCache.liveImageCount, 1); + + // NetworkImage should not make another request. + mockHttpClient.request.response.statusCode = HttpStatus.badRequest; + + final Completer imageAvailable2 = Completer(); + final ImageProvider imageProvider2 = NetworkImage( + nonconst('testing.url'), + headers: nonconst({'key': 'value'}), + ); + + final ImageStream result2 = imageProvider2.resolve(ImageConfiguration.empty); + result2.addListener( + ImageStreamListener((ImageInfo image, bool synchronousCall) { + imageAvailable2.complete(); + }), + ); + await imageAvailable2.future; + expect(imageCache.liveImageCount, 1); + + debugNetworkImageHttpClientProvider = null; + }, skip: isBrowser); // [intended] Browser does not resolve images this way. + test('Network image sets tag', () async { const String url = 'http://test.png'; const int chunkSize = 8; @@ -340,6 +385,9 @@ class _FakeHttpClient extends Fake implements HttpClient { class _FakeHttpClientRequest extends Fake implements HttpClientRequest { final _FakeHttpClientResponse response = _FakeHttpClientResponse(); + @override + final HttpHeaders headers = _FakeHttpHeaders(); + @override Future close() async { return response; @@ -379,3 +427,8 @@ class _FakeHttpClientResponse extends Fake implements HttpClientResponse { return futureValue ?? futureValue as E; // Mirrors the implementation in Stream. } } + +class _FakeHttpHeaders extends Fake implements HttpHeaders { + @override + void add(String name, Object value, {bool preserveHeaderCase = false}) {} +}