-
Notifications
You must be signed in to change notification settings - Fork 29.3k
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
base: master
Are you sure you want to change the base?
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
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. |
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.
Fix a bug introduced by 97aae2a (#171916), where
hashCode
is giving different results for values which==
says are equal. Which broke caching for uses ofImage.network
widget whenheaders
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.