-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Fix Image.network not using cache when headers are specified #176831
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Image.network not using cache when headers are specified #176831
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request corrects the hashCode implementation for NetworkImage in both its IO and web versions. The change addresses a bug where the hash code for the headers map was computed based on object identity, leading to cache misses for Image.network when new but identical header maps were used. By using MapEquality from the collection package, the hash code is now correctly based on the map's contents, aligning it with the operator== implementation. A new test is also added to verify that NetworkImage with identical arguments correctly utilizes the image cache, preventing future regressions.
cb3ec5b to
dd58403
Compare
gnprice
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @rajveermalviya for the diagnosis and the fix! These changes look good; just nits below on the test.
I also tweaked the description (which becomes the merged commit message) to point to the previous PR as well as the commit, for ease of cross-reference.
| // NetworkImage should not make another request. | ||
| mockHttpClient.thrownError = Error(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line doesn't totally reassure me that the test will notice if the NetworkImage does make another request. With the current implementation of _FakeHttpClient, that holds because thrownError takes precedence over request; but there's no reason it has to be that way, and someone could well refactor that in the future without noticing that it causes this test to be ineffective.
Let's instead put this in the same terms as the .request.response setup above, so that it clearly overrides it:
| // NetworkImage should not make another request. | |
| mockHttpClient.thrownError = Error(); | |
| // NetworkImage should not make another request. | |
| mockHttpClient.request.response.statusCode = HttpStatus.badRequest; |
Alternatively, could set debugNetworkImageHttpClientProvider to a fresh _FakeHttpClient instance.
| class _FakeHttpClientResponse extends Fake implements HttpClientResponse { | ||
| bool drained = false; | ||
|
|
||
| @override | ||
| final HttpHeaders headers = _FakeHttpHeaders(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change isn't needed, is it? The new test adds headers only in the request, not the response.
Fix a bug introduced by 97aae2a, 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.
dd58403 to
6efc307
Compare
|
Thanks for the review @gnprice! Addressed those comments. |
gnprice
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! These changes LGTM.
BTW for future PRs in this repo, the most convenient thing is to push revisions as added commits on top of the existing ones. (The exception is when there's a need to rebase atop a newer main.)
That's helpful for later reviewers or other readers to be able to follow the thread in the GitHub web interface, because it effectively keeps the full history of your changes visible here. On merge, the changes will get squashed into one commit anyway, so there's no harm to the clarity of the final history in main.
|
autosubmit label was removed for flutter/flutter/176831, because The base commit of the PR is older than 7 days and can not be merged. Please merge the latest changes from the main into this branch and resubmit the PR. |
dkwingsmt
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thank you!
|
autosubmit label was removed for flutter/flutter/176831, because - The status or check suite Google testing has failed. Please fix the issues identified (or deflake) before re-applying this label. |
flutter/flutter@891d7d5...2d34167 2025-10-20 [email protected] Cleanup create_updated_flutter_deps.py a bit (flutter/flutter#177162) 2025-10-20 [email protected] Fixed hot reload/restart crashes after closing browser tab on web-server device (flutter/flutter#177026) 2025-10-20 [email protected] Roll Skia from 0a3ace6fde82 to ed4294faecde (2 revisions) (flutter/flutter#177249) 2025-10-20 [email protected] Add DropdownMenu.decorationBuilder (flutter/flutter#176264) 2025-10-20 [email protected] Roll Skia from 05e2f42f533d to 0a3ace6fde82 (1 revision) (flutter/flutter#177242) 2025-10-20 [email protected] Roll Skia from 89abc5393317 to 05e2f42f533d (1 revision) (flutter/flutter#177238) 2025-10-20 [email protected] [ios][engine] Fix autofill context cleanup and view lifecycle management (flutter/flutter#173598) 2025-10-20 [email protected] Fix Image.network not using cache when headers are specified (flutter/flutter#176831) 2025-10-19 [email protected] Roll Dart SDK from a66f334fee2a to 2cd2106f2cef (4 revisions) (flutter/flutter#177190) 2025-10-19 [email protected] Roll Skia from 2d424175a481 to 89abc5393317 (1 revision) (flutter/flutter#177235) 2025-10-19 [email protected] Make sure that a ListTile doesn't crash in 0x0 environment (flutter/flutter#176176) 2025-10-19 [email protected] Make sure that a DropdownButton doesn't crash in 0x0 environment (flutter/flutter#174880) 2025-10-19 [email protected] Roll Skia from 899155871d29 to 2d424175a481 (1 revision) (flutter/flutter#177229) 2025-10-19 [email protected] Roll Skia from b864c56efb66 to 899155871d29 (1 revision) (flutter/flutter#177227) 2025-10-19 [email protected] Roll Fuchsia Linux SDK from M8WT2GMY46e_0fFho... to tKrvmvTOQITL81oOC... (flutter/flutter#177223) 2025-10-19 [email protected] Roll Skia from 0992b560454f to b864c56efb66 (1 revision) (flutter/flutter#177222) 2025-10-18 [email protected] Fix HEIF decoding (flutter/flutter#176860) 2025-10-18 [email protected] Roll Skia from 74df18176924 to 0992b560454f (1 revision) (flutter/flutter#177217) If this roll has caused a breakage, revert this CL and stop the roller using the controls here: https://autoroll.skia.org/r/flutter-packages Please CC [email protected],[email protected] on the revert to ensure that a human is aware of the problem. To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose To report a problem with the AutoRoller itself, please file a bug: https://issues.skia.org/issues/new?component=1389291&template=1850622 Documentation for the AutoRoller is here: https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
…#176831) Fix a bug introduced by 97aae2a (flutter#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
Fix a bug introduced by 97aae2a (#171916), where
hashCodeis giving different results for values which==says are equal. Which broke caching for uses ofImage.networkwidget whenheadersmap was passed.Pre-launch Checklist
///).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. Comments from the
gemini-code-assistbot 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.