DynamicCachedFonts class Null safety

Allows dynamically loading fonts from the given url.

Fetching fonts from Firebase Storage is also supported.

For ready to use, simple interface, initialize DynamicCachedFonts and call the load method either in you initState() or just before runApp() itself.

class _SomeStateFulWidgetState extends State<_SomeStateFulWidget> {
  @override
  void initState() {
    const DynamicCachedFonts dynamicCachedFont = DynamicCachedFonts(
      fontFamily: 'font',
      url: // Add url to a font
    );
    dynamicCachedFont.load();

    super.initState();
  }
void main() async {
  final DynamicCachedFont dynamicCachedFont = DynamicCachedFont(
    fontFamily: 'font',
    url: // Add url to a font
  );
  dynamicCachedFont.load();

  runApp(
    YourAppHere(
      ...
      TextStyle(
        fontFamily: 'font'
      ),
      ...
    ),
  );
}

For greater customization, use static methods.

Constructors

DynamicCachedFonts({required String url, required String fontFamily, int maxCacheObjects = kDefaultMaxCacheObjects, Duration cacheStalePeriod = kDefaultCacheStalePeriod})
Allows dynamically loading fonts from the given url and caching them. [...]
DynamicCachedFonts.family({required List<String> urls, required String fontFamily, int maxCacheObjects = kDefaultMaxCacheObjects, Duration cacheStalePeriod = kDefaultCacheStalePeriod})
Allows dynamically loading fonts from the given list of url and caching them. The fontFamily groups a series of related font assets, each of which defines how to render a specific FontWeight and FontStyle within the family. [...]
DynamicCachedFonts.fromFirebase({required String bucketUrl, required String fontFamily, int maxCacheObjects = kDefaultMaxCacheObjects, Duration cacheStalePeriod = kDefaultCacheStalePeriod})
Allows dynamically loading fonts from firebase storage with the given firebase storage url, and caching them. [...]

Properties

cacheStalePeriod Duration
The time duration in which a cache object is considered 'stale'. When a file is cached but not being used for a certain time the file will be deleted [...]
final
fontFamily String
Used to specify the name of the font family which is to be used as TextStyle.fontFamily.
final
hashCode int
The hash code for this object. [...]
read-only, inherited
maxCacheObjects int
Defines how large the cache is allowed to be. [...]
final
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited
urls List<String>
Used to specify the download url(s) for the required font(s). [...]
final

Methods

load() Future<Iterable<FileInfo>>
Used to download and load a font into the app with the given url and cache configuration. [...]
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
toString() String
A string representation of this object. [...]
inherited

Operators

operator ==(Object other) bool
The equality operator. [...]
inherited

Static Methods

cacheFont(String url, {Duration cacheStalePeriod = kDefaultCacheStalePeriod, int maxCacheObjects = kDefaultMaxCacheObjects}) Future<FileInfo>
Downloads and caches font from the url with the given configuration. [...]
canLoadFont(String url) Future<bool>
Checks whether the given url can be loaded directly from cache. [...]
custom({required CacheManager cacheManager, bool force = false}) → void
Accepts cacheManager and force to provide a custom CacheManager for testing. [...]
loadCachedFamily(List<String> urls, {required String fontFamily, FontLoader? fontLoader}) Future<Iterable<FileInfo>>
Fetches the given urls from cache and loads them into the engine to be used. [...]
loadCachedFont(String url, {required String fontFamily, FontLoader? fontLoader}) Future<FileInfo>
Fetches the given url from cache and loads it as an asset. [...]
removeCachedFont(String url) Future<void>
Removes the given url can be loaded directly from cache. [...]
toggleVerboseLogging(bool shouldVerboseLog) → void
Used to specify whether detailed logs should be printed for debugging. [...]