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.

<!-- Links -->
[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
This commit is contained in:
Rajesh Malviya 2025-10-20 07:35:21 +05:30 committed by GitHub
parent 0eaaf19086
commit d939d6b00a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 62 additions and 2 deletions

View File

@ -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<image_provider.NetworkIm
}
@override
int get hashCode => Object.hash(url, scale, headers);
int get hashCode => Object.hash(url, scale, const MapEquality<String, String>().hash(headers));
@override
String toString() =>

View File

@ -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<image_provider.NetworkIm
}
@override
int get hashCode => Object.hash(url, scale, webHtmlElementStrategy, headers);
int get hashCode => Object.hash(
url,
scale,
webHtmlElementStrategy,
const MapEquality<String, String>().hash(headers),
);
@override
String toString() =>

View File

@ -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>[Uint8List.fromList(kTransparentImage)];
final Completer<void> imageAvailable1 = Completer<void>();
final ImageProvider imageProvider1 = NetworkImage(
nonconst('testing.url'),
headers: nonconst(<String, String>{'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<void> imageAvailable2 = Completer<void>();
final ImageProvider imageProvider2 = NetworkImage(
nonconst('testing.url'),
headers: nonconst(<String, String>{'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<HttpClientResponse> 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}) {}
}