Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Use when preloading fonts, asset/network images, Lottie/Rive animations, local JSON/config, warming initial API data, or optimizing Flutter Web startup.
.claude/skills/evanca-flutter-pre-caching/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-01 | ✗→✓ | ▲ Improved | 146% | 0% |
| case-08 | ✗→✓ | ▲ Improved | 183% | 0% |
| case-11 | ✗→✓ | ▲ Improved | 195% | 0% |
| case-13 | ✗→✓ | ▲ Improved | 475% | 0% |
| case-14 | ✗→✓ | ▲ Improved | 231% | 0% |
Pre-caching helps avoid jank, loading flashes, font swaps, and delayed first renders.
The key rule:
> Pre-cache only what the user will likely see in the next 1 to 2 screens.
Do not pre-cache the whole app. That can slow startup and waste memory.
Use GoogleFonts.pendingFonts() to load the font variants before showing text.
dartimport 'package:flutter/material.dart'; import 'package:google_fonts/google_fonts.dart'; class ExampleSimple extends StatefulWidget { const ExampleSimple({super.key}); @override State<ExampleSimple> createState() => _ExampleSimpleState(); } class _ExampleSimpleState extends State<ExampleSimple> { late final Future<List<void>> googleFontsPending; @override void initState() { super.initState(); googleFontsPending = GoogleFonts.pendingFonts([ GoogleFonts.poppins(), GoogleFonts.montserrat(fontStyle: FontStyle.italic), ]); } @override Widget build(BuildContext context) { final pushButtonTextStyle = GoogleFonts.poppins( textStyle: Theme.of(context).textTheme.headlineMedium, ); final counterTextStyle = GoogleFonts.montserrat( fontStyle: FontStyle.italic, textStyle: Theme.of(context).textTheme.displayLarge, ); return FutureBuilder<List<void>>( future: googleFontsPending, builder: (context, snapshot) { if (snapshot.connectionState != ConnectionState.done) { return const SizedBox(); } return Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( 'You have pushed the button this many times:', style: pushButtonTextStyle, ), Text( '0', style: counterTextStyle, ), ], ); }, ); } }
For production and offline support, prefer bundling critical fonts as assets.
yamlflutter: fonts: - family: AppFont fonts: - asset: assets/fonts/AppFont-Regular.ttf - asset: assets/fonts/AppFont-Bold.ttf weight: 700
Use it in the app theme:
dartMaterialApp( theme: ThemeData( fontFamily: 'AppFont', ), )
Use precacheImage for images that appear soon.
dart@override void didChangeDependencies() { super.didChangeDependencies(); precacheImage( const AssetImage('assets/images/header.png'), context, ); }
Then use the image normally:
dartImage.asset('assets/images/header.png')
Use precacheImage with NetworkImage.
dart@override void didChangeDependencies() { super.didChangeDependencies(); precacheImage( const NetworkImage('https://example.com/image.png'), context, ); }
Then use it normally:
dartImage.network('https://example.com/image.png')
Important: precacheImage warms Flutter’s in-memory image cache. It does not provide long-term offline caching.
For disk caching, use a package like cached_network_image:
yamldependencies: cached_network_image: ^latest
Example:
dartCachedNetworkImage( imageUrl: 'https://example.com/image.png', )
dart@override void didChangeDependencies() { super.didChangeDependencies(); final images = <ImageProvider>[ const AssetImage('assets/images/header.png'), const AssetImage('assets/images/avatar.png'), const NetworkImage('https://example.com/banner.png'), ]; for (final image in images) { precacheImage(image, context); } }
precacheImage returns a Future<void>, so you can wait before rendering the real UI.
dartlate Future<void> _preloadImagesFuture; @override void didChangeDependencies() { super.didChangeDependencies(); _preloadImagesFuture = Future.wait([ precacheImage( const AssetImage('assets/images/header.png'), context, ), precacheImage( const NetworkImage('https://example.com/banner.png'), context, ), ]); } @override Widget build(BuildContext context) { return FutureBuilder<void>( future: _preloadImagesFuture, builder: (context, snapshot) { if (snapshot.connectionState != ConnectionState.done) { return const CircularProgressIndicator(); } return Column( children: [ Image.asset('assets/images/header.png'), Image.network('https://example.com/banner.png'), ], ); }, ); }
> !NOTE] > Use didChangeDependencies, not initState, when you need context for precacheImage.
Good candidates:
Bad candidates:
dartclass AppPreloader { const AppPreloader(); Future<void> preload(BuildContext context) async { await Future.wait([ _precacheImages(context), _loadCriticalAssets(), _warmUpInitialData(), ]); } Future<void> _precacheImages(BuildContext context) { return Future.wait([ precacheImage( const AssetImage('assets/images/home_hero.png'), context, ), precacheImage( const AssetImage('assets/images/logo.png'), context, ), ]); } Future<void> _loadCriticalAssets() async { await rootBundle.loadString('assets/config/app_config.json'); } Future<void> _warmUpInitialData() async { // Example: // await userRepository.getCurrentUser(); } }
Usage:
dartlate Future<void> _preloadFuture; @override void didChangeDependencies() { super.didChangeDependencies(); _preloadFuture = const AppPreloader().preload(context); }
With FutureBuilder:
dart@override Widget build(BuildContext context) { return FutureBuilder<void>( future: _preloadFuture, builder: (context, snapshot) { if (snapshot.connectionState != ConnectionState.done) { return const SplashScreen(); } return const HomeScreen(); }, ); }
Useful for feature flags, local app config, mock data, translations, or onboarding content.
dartimport 'package:flutter/services.dart'; final configJson = await rootBundle.loadString( 'assets/config/app_config.json', );
Example:
dartclass AppConfigLoader { Future<String> load() { return rootBundle.loadString('assets/config/app_config.json'); } }
This is not pre-caching in the Flutter image-cache sense, but it is often the most useful startup optimization.
dartfinal userFuture = userRepository.getCurrentUser(); final dashboardFuture = dashboardRepository.getDashboard();
Example:
dartclass StartupData { const StartupData({ required this.user, required this.dashboard, }); final User user; final Dashboard dashboard; } Future<StartupData> loadStartupData() async { final results = await Future.wait([ userRepository.getCurrentUser(), dashboardRepository.getDashboard(), ]); return StartupData( user: results[0] as User, dashboard: results[1] as Dashboard, ); }
If the animation appears immediately, preload it.
dartfinal composition = await AssetLottie( 'assets/animations/success.json', ).load();
Use this only for animations shown early. Avoid preloading many large animations on app start.
Rather than triggering the asset load during a build (which causes jank), preload the RiveFile byte data in advance:
dartimport 'package:flutter/services.dart'; import 'package:rive/rive.dart'; // Preload the RiveFile final data = await rootBundle.load('assets/animations/character.riv'); final riveFile = RiveFile.import(data); // Display using direct constructor to avoid initialization delays: RiveAnimation.direct(riveFile);
This ensures that the animation renders immediately upon widget mount.
Most apps do not need this. It can help if you have:
Basic shape:
dartclass CustomShaderWarmUp extends ShaderWarmUp { @override void warmUpOnCanvas(Canvas canvas) { final paint = Paint(); canvas.drawRect( const Rect.fromLTWH(0, 0, 100, 100), paint, ); } }
Then set it before runApp:
dartvoid main() { PaintingBinding.instance.shaderWarmUp = CustomShaderWarmUp(); runApp(const MyApp()); }
Use this only when profiling shows shader compilation jank.
On Flutter Web, think less:
Think more:
For critical first-screen images, use both browser preload and Flutter precacheImage.
In web/index.html:
html<link rel="preload" href="assets/assets/images/home_hero.png" as="image" >
In Flutter:
dartawait precacheImage( const AssetImage('assets/images/home_hero.png'), context, );
Why both?
precacheImage warms Flutter's image cache.In web/index.html:
html<link rel="preload" href="https://example.com/banner.png" as="image" >
In Flutter:
dartawait precacheImage( const NetworkImage('https://example.com/banner.png'), context, );
Only do this for images the user is almost guaranteed to see.
Declare the font in pubspec.yaml:
yamlflutter: fonts: - family: AppFont fonts: - asset: assets/fonts/AppFont-Regular.ttf - asset: assets/fonts/AppFont-Bold.ttf weight: 700
Preload the critical font in web/index.html:
html<link rel="preload" href="assets/assets/fonts/AppFont-Regular.ttf" as="font" type="font/ttf" crossorigin >
Use it in Flutter:
dartThemeData( fontFamily: 'AppFont', )
Usually preload:
Avoid preloading every weight and italic variant unless the first screen needs them.
Emojis can trigger font fallback work.
Example:
dartText('Welcome 👋 🎉')
On web, this can cause:
Simple and effective:
dartText('Welcome')
Then show emojis after the app has loaded:
dartText('Welcome 👋')
This is often the best option if emojis are decorative.
Add the font to pubspec.yaml:
yamlflutter: fonts: - family: NotoColorEmoji fonts: - asset: assets/fonts/NotoColorEmoji.ttf
Preload it in web/index.html:
html<link rel="preload" href="assets/assets/fonts/NotoColorEmoji.ttf" as="font" type="font/ttf" crossorigin >
Use it where needed:
dartconst Text( '👋 🎉 ❤️', style: TextStyle( fontFamily: 'NotoColorEmoji', ), )
Important:
Good cases:
Bad cases:
Rule:
Flutter Web startup can also include renderer assets, for example CanvasKit or Skwasm files.
Usually, you do not preload these from Dart. You configure the renderer through Flutter Web initialization.
Example in web bootstrap:
js_flutter.loader.load({ config: { renderer: 'canvaskit', }, });
Other configs can include things like:
js_flutter.loader.load({ config: { renderer: 'canvaskit', canvasKitBaseUrl: '/canvaskit/', }, });
Most apps should let Flutter manage this unless there is a clear hosting or performance reason to change it.
Usually preload:
Usually avoid:
textassets/ images/ logo.png home_hero.png onboarding_hero.png fonts/ AppFont-Regular.ttf AppFont-Bold.ttf NotoColorEmoji.ttf config/ app_config.json animations/ success.json character.riv
yamlflutter: assets: - assets/images/ - assets/config/ - assets/animations/ fonts: - family: AppFont fonts: - asset: assets/fonts/AppFont-Regular.ttf - asset: assets/fonts/AppFont-Bold.ttf weight: 700 - family: NotoColorEmoji fonts: - asset: assets/fonts/NotoColorEmoji.ttf
html<!-- App fonts --> <link rel="preload" href="assets/assets/fonts/AppFont-Regular.ttf" as="font" type="font/ttf" crossorigin > <link rel="preload" href="assets/assets/fonts/AppFont-Bold.ttf" as="font" type="font/ttf" crossorigin > <!-- Only if emojis are critical to first paint --> <link rel="preload" href="assets/assets/fonts/NotoColorEmoji.ttf" as="font" type="font/ttf" crossorigin > <!-- Critical images --> <link rel="preload" href="assets/assets/images/logo.png" as="image" > <link rel="preload" href="assets/assets/images/home_hero.png" as="image" >
> !NOTE] > Flutter web asset URLs often include assets/assets/... because Flutter serves declared assets under the assets/ path, while your asset path also starts with assets/. > > For example: > - Flutter asset: assets/images/logo.png > - Web URL: assets/assets/images/logo.png
dartimport 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:google_fonts/google_fonts.dart'; class AppStartupPreloader { const AppStartupPreloader(); Future<void> preload(BuildContext context) async { await Future.wait([ _preloadFonts(), _preloadImages(context), _preloadLocalConfig(), _preloadStartupData(), ]); } Future<void> _preloadFonts() { return GoogleFonts.pendingFonts([ GoogleFonts.inter(), GoogleFonts.inter(fontWeight: FontWeight.w700), ]); } Future<void> _preloadImages(BuildContext context) { return Future.wait([ precacheImage( const AssetImage('assets/images/logo.png'), context, ), precacheImage( const AssetImage('assets/images/home_hero.png'), context, ), ]); } Future<void> _preloadLocalConfig() async { await rootBundle.loadString('assets/config/app_config.json'); } Future<void> _preloadStartupData() async { // Start your critical first API calls here. // // Example: // await userRepository.getCurrentUser(); // await dashboardRepository.getDashboard(); } }
Usage:
dartclass StartupGate extends StatefulWidget { const StartupGate({super.key}); @override State<StartupGate> createState() => _StartupGateState(); } class _StartupGateState extends State<StartupGate> { Future<void>? _startupFuture; @override void didChangeDependencies() { super.didChangeDependencies(); _startupFuture ??= const AppStartupPreloader().preload(context); } @override Widget build(BuildContext context) { return FutureBuilder<void>( future: _startupFuture, builder: (context, snapshot) { if (snapshot.connectionState != ConnectionState.done) { return const SplashScreen(); } return const HomeScreen(); }, ); } }
Pre-cache this:
Do not pre-cache this:
For Flutter Web specifically:
precacheImage for Flutter image cache.Other measured skills in the registry, with their headline benchmark lift.