loadStream method

Stream<FileInfo> loadStream({
  1. ItemCountProgressListener? itemCountProgressListener,
  2. DownloadProgressListener? downloadProgressListener,
})

Used to download and load a font into the app with the given urls and cache configuration. A stream of FileInfo object(s) is returned which emits the font files as they are loaded. The total number of files returned corresponds to the number of urls provided.

  • The itemCountProgressListener property is used to listen to the download progress of the fonts. It gives information about the number of files downloaded and the total number of files to be downloaded. It is called with

    a double value which indicates the fraction of items that have been downloaded, an int value which indicates the total number of items to be downloaded and, another int value which indicates the number of items that have been downloaded so far.

  • The downloadProgressListener property is used to listen to the download progress of the font. It is called with a DownloadProgress object which contains information about the download progress.

Implementation

Stream<FileInfo> loadStream({
  ItemCountProgressListener? itemCountProgressListener,
  DownloadProgressListener? downloadProgressListener,
}) async* {
  if (_loaded) throw StateError('Font has already been loaded');
  _loaded = true;

  WidgetsFlutterBinding.ensureInitialized();

  final List<String> downloadUrls = await Future.wait(
    urls.map(
      (String url) async => _isFirebaseURL ? await Utils.handleUrl(url) : url,
    ),
  );

  final Stream<FileInfo> fontStream = loadCachedFamilyStream(
    downloadUrls,
    fontFamily: fontFamily,
    progressListener: itemCountProgressListener,
  );

  try {
    await for (final font in fontStream) {
      yield font;

      // Checks whether any of the files is invalid.
      // The validity is determined by parsing headers returned when the file was
      // requested. The date/time is a file validity guarantee by the source.
      // This was done to preserve `Cachemanager.getSingleFile`'s behaviour.
      if (font.validTill.isBefore(DateTime.now())) {
        devLog([
          'Font file expired on ${font.validTill}.',
          'Downloading font from ${font.originalUrl}...',
        ]);
        cacheFontStream(
          font.originalUrl,
          cacheStalePeriod: cacheStalePeriod,
          maxCacheObjects: maxCacheObjects,
          progressListener: downloadProgressListener,
        ).listen((_) {});
      }
    }
  } catch (_) {
    devLog(<String>['Font is not in cache.', 'Loading font now...']);

    await Future.wait([
      for (final String url in downloadUrls)
        cacheFontStream(
          url,
          cacheStalePeriod: cacheStalePeriod,
          maxCacheObjects: maxCacheObjects,
          progressListener: downloadProgressListener,
        ).listen((_) {}).asFuture<void>()
    ]);

    yield* loadCachedFamilyStream(
      downloadUrls,
      fontFamily: fontFamily,
      progressListener: itemCountProgressListener,
    );
  }
}