Skip to content

Commit ac87e65

Browse files
[google_fonts] Remove failed loads from pendingFonts (#12240)
Failed font loads remain in `pendingFontFutures` because the current cleanup callback runs only after successful completion. As a result, every later `GoogleFonts.pendingFonts()` call rethrows the same completed error. This change removes a tracked load on either completion path while preserving the original error for the caller currently awaiting `pendingFonts()`. It adds regression coverage showing that the first call reports the load failure and a later call is no longer poisoned by it. Fixes flutter/flutter#182430. ## Pre-Review Checklist **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. [^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.
1 parent a5beb57 commit ac87e65

4 files changed

Lines changed: 23 additions & 4 deletions

File tree

‎packages/google_fonts/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 8.2.1
2+
3+
- Fixes `pendingFonts()` retaining failed font loads.
4+
15
## 8.2.0
26

37
- Extract the class `Config` to its own file and rename it `GoogleFontsConfig`. The `Config` class is now deprecated.

‎packages/google_fonts/lib/src/google_fonts_base.dart‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ void clearCache() => _loadedFonts.clear();
2828

2929
/// Set of [Future]s corresponding to fonts that are loading.
3030
///
31-
/// When a font is loading, a future is added to this set. When it is loaded in
32-
/// the [FontLoader], that future is removed from this set.
31+
/// When a font is loading, a future is added to this set. When the load
32+
/// completes, whether successfully or with an error, that future is removed.
3333
final Set<Future<void>> pendingFontFutures = <Future<void>>{};
3434

3535
/// Default client used to fetch fonts when one is not supplied.
@@ -106,7 +106,7 @@ TextStyle googleFontsTextStyle({
106106

107107
final Future<void> loadingFuture = loadFontIfNecessary(descriptor);
108108
pendingFontFutures.add(loadingFuture);
109-
loadingFuture.then((_) => pendingFontFutures.remove(loadingFuture));
109+
loadingFuture.whenComplete(() => pendingFontFutures.remove(loadingFuture)).ignore();
110110

111111
return textStyle.copyWith(
112112
fontFamily: familyWithVariant.toString(),

‎packages/google_fonts/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: google_fonts
22
description: A Flutter package to use fonts from fonts.google.com. Supports HTTP fetching, caching, and asset bundling.
33
repository: https://github.com/flutter/packages/tree/main/packages/google_fonts
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+google_fonts%22
5-
version: 8.2.0
5+
version: 8.2.1
66

77
environment:
88
sdk: ^3.10.0

‎packages/google_fonts/test/load_font_if_necessary_test.dart‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ final GoogleFontsDescriptor fakeDescriptor = GoogleFontsDescriptor(
8888
file: _fakeResponseFile,
8989
);
9090

91+
final Map<GoogleFontsVariant, GoogleFontsFile> _fakeFonts = <GoogleFontsVariant, GoogleFontsFile>{
92+
fakeDescriptor.familyWithVariant.googleFontsVariant: fakeDescriptor.file,
93+
};
94+
9195
// Same family & variant, different file.
9296
final GoogleFontsDescriptor fakeDescriptorDifferentFile = GoogleFontsDescriptor(
9397
familyWithVariant: fakeDescriptor.familyWithVariant,
@@ -361,4 +365,15 @@ void main() {
361365
returnsNormally,
362366
);
363367
});
368+
369+
test('pendingFonts removes failed font loads', () async {
370+
when(mockHttpClient.gets(any)).thenAnswer((_) async {
371+
return http.Response('', 404);
372+
});
373+
374+
googleFontsTextStyle(fontFamily: fakeDescriptor.familyWithVariant.family, fonts: _fakeFonts);
375+
376+
await expectLater(GoogleFonts.pendingFonts(), throwsException);
377+
expect(await GoogleFonts.pendingFonts(), isEmpty);
378+
});
364379
}

0 commit comments

Comments
 (0)