Skip to content

Conversation

rajveermalviya
Copy link
Contributor

@rajveermalviya rajveermalviya commented Oct 10, 2025

Fix a bug introduced by 97aae2a (#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

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-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.

@github-actions github-actions bot added the framework flutter/packages/flutter repository. See also f: labels. label Oct 10, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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.

@rajveermalviya rajveermalviya force-pushed the pr-networkimage-hashcode branch from cb3ec5b to dd58403 Compare October 10, 2025 13:34
Copy link
Member

@gnprice gnprice left a 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.

Comment on lines 315 to 316
// NetworkImage should not make another request.
mockHttpClient.thrownError = Error();
Copy link
Member

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:

Suggested change
// 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.

Comment on lines 397 to 401
class _FakeHttpClientResponse extends Fake implements HttpClientResponse {
bool drained = false;

@override
final HttpHeaders headers = _FakeHttpHeaders();
Copy link
Member

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.
@rajveermalviya rajveermalviya force-pushed the pr-networkimage-hashcode branch from dd58403 to 6efc307 Compare October 10, 2025 19:27
@rajveermalviya
Copy link
Contributor Author

Thanks for the review @gnprice! Addressed those comments.

Copy link
Member

@gnprice gnprice left a 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants