[google_font] Move the httpClient from a package level variable, to be on the Config object#11026
[google_font] Move the httpClient from a package level variable, to be on the Config object#11026bramp wants to merge 0 commit intoflutter:mainfrom
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors the google_fonts package to move the httpClient from a package-level variable into the GoogleFonts.config object. This is a positive change that improves the package's testability and allows users to provide a custom http.Client. The changes are applied consistently across the library and its tests. My feedback focuses on preventing a potential resource leak with the new httpClient implementation.
| /// The HTTP client used to fetch fonts. | ||
| http.Client httpClient = http.Client(); |
There was a problem hiding this comment.
This is a great change for testability and customization! However, the current implementation creates an http.Client that is never closed, which can lead to resource leaks.
A better approach is to make httpClient nullable. This clarifies ownership: if a user provides a client, they are responsible for closing it. If it's null, the library can manage the client's lifecycle.
Here's a suggested implementation for the Config class:
/// The HTTP client used to fetch fonts.
///
/// If this is null, a new [http.Client] will be created for each request.
/// If you are making many font requests, it is recommended to provide your
/// own long-lived [http.Client].
///
/// If you supply a client, you are responsible for closing it.
http.Client? httpClient;Then, in _httpFetchFontAndSaveToDevice in google_fonts_base.dart, you would handle the nullable client like this to ensure it's closed if created on-the-fly:
// in _httpFetchFontAndSaveToDevice
final client = GoogleFonts.config.httpClient ?? http.Client();
try {
response = await client.get(uri);
} finally {
if (GoogleFonts.config.httpClient == null) {
// We created this client, so we should close it.
client.close();
}
} /// The HTTP client used to fetch fonts.
///
/// If this is null, a new [http.Client] will be created for each request.
/// If you are making many font requests, it is recommended to provide your
/// own long-lived [http.Client].
///
/// If you supply a client, you are responsible for closing it.
http.Client? httpClient;
In progress: I have not written changelog/documentation updates. I want to get early feedback / validation on this idea before I do the remaining work.
Move the httpClient from a package level variable, to be on the Config object
This allows the user to customise the httpClient as well as mock it out in tests.
Fixes #182429
Pre-Review Checklist
[shared_preferences]pubspec.yamlwith an appropriate new version according to the pub versioning philosophy, or I have commented below to indicate which version change exemption this PR falls under1.CHANGELOG.mdto add a description of the change, following repository CHANGELOG style, or I have commented below to indicate which CHANGELOG exemption this PR falls under1.///).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.Footnotes
Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2 ↩3