Skip to content

[google_font] Move the httpClient from a package level variable, to be on the Config object#11026

Closed
bramp wants to merge 0 commit intoflutter:mainfrom
bramp:main
Closed

[google_font] Move the httpClient from a package level variable, to be on the Config object#11026
bramp wants to merge 0 commit intoflutter:mainfrom
bramp:main

Conversation

@bramp
Copy link
Contributor

@bramp bramp commented Feb 14, 2026

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

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.

Footnotes

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

@bramp bramp requested a review from Piinks as a code owner February 14, 2026 21:39
@github-actions github-actions bot added p: google_fonts triage-framework Should be looked at in framework triage labels Feb 14, 2026
Copy link

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

Comment on lines 49 to 50
/// The HTTP client used to fetch fonts.
http.Client httpClient = http.Client();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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;

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

Labels

p: google_fonts triage-framework Should be looked at in framework triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[google_fonts] Allow the HTTP client to be configured

1 participant