pendingFonts static method

Future<List<void>> pendingFonts([
  1. List? _
])

Returns a Future which resolves when requested fonts have finished loading and are ready to be rendered on screen.

Usage:

GoogleFonts.lato();
GoogleFonts.pacificoTextTheme();
await GoogleFonts.pendingFonts(); // <-- waits until Lato and Pacifico files have loaded.

To keep things tidy, on can also pass in requested fonts as a list to pendingFonts.

await GoogleFonts.pendingFonts([
  GoogleFonts.lato(),
  GoogleFonts.pacificoTextTheme()
]);

To avoid visual font swaps that occur when a font is loading, consider using FutureBuilder. Note: This future cannot be created in build, as described in FutureBuilder's documentation.

late Future googleFontsPending;

@override
void initState() {
  super.initState();
  googleFontsPending = GoogleFonts.pendingFonts([
    ...
  ]);
}

@override
Widget build(BuildContext context) {
  return FutureBuilder(
    future: googleFontsPending,
    builder: (context, snapshot) {
      if (snapshot.connectionState != ConnectionState.done) {
        return const SizedBox();
      }
      ...
    }
  );
}

Implementation

static Future<List<void>> pendingFonts([List<dynamic>? _]) =>
    Future.wait(pendingFontFutures);