loadFont function

Future<void> loadFont(
  1. String family,
  2. List<String> fromPaths
)

Loads fonts from the given fromPaths into the Flutter engine under the specified family.

Implementation

Future<void> loadFont(String family, List<String> fromPaths) async {
  if (fromPaths.isEmpty) {
    return;
  }

  await maybeRunAsync(() async {
    final fontLoader = FontLoader(family);
    for (final path in fromPaths) {
      try {
        final file = File(path);
        if (file.existsSync()) {
          final bytes = file.readAsBytesSync();
          fontLoader.addFont(Future.value(bytes.buffer.asByteData()));
        } else {
          final data = rootBundle.load(path);
          fontLoader.addFont(Future.value(data));
        }
      } catch (e, _) {
        debugPrint("Could not load font $path: $e");
      }
    }

    await fontLoader.load();
  });
}