Description / Motivation
Developers attempting to precache vector graphics or SVGs to prevent route transition jank often run into cache misses and performance regressions.
There are two primary caching bottlenecks:
- Theme-based Cache Misses: In
flutter_svg, svg.cache keys include the SvgTheme derived from BuildContext (DefaultSvgTheme.of(context)). If a developer precaches early by calling loader.loadBytes(null), the cache key uses a default theme. When the widget is later rendered inside a MaterialApp route hierarchy with different inherited text styles or font sizes, the cache key mismatches, causing svg.cache to miss and re-fetch/re-parse everything on the fly.
- Ephemeral Live Picture Cache: In
vector_graphics, the internal _livePictureCache is reference-counted (count == 0 when no widgets are actively mounted). Therefore, preloading bytes doesn't keep the decoded ui.Picture alive across route transitions.
Providing dedicated precacheVectorGraphic and precacheSvg functions (analogous to Flutter's precacheImage) would allow developers to correctly warm up both the byte cache and the live picture cache using the target BuildContext.
Proposed API
Add utility functions in both packages:
// In package:vector_graphics
Future<void> precacheVectorGraphic(
BytesLoader loader,
BuildContext context, {
VectorGraphicsErrorListener? onError,
}) async {
// Resolves bytes, decodes PictureInfo using context (locale/directionality),
// and seeds _livePictureCache with an initial reference count or warm state.
}
// In package:flutter_svg
Future<void> precacheSvg(
SvgLoader loader,
BuildContext context, {
VectorGraphicsErrorListener? onError,
}) async {
// Derives the correct SvgTheme from context, resolves bytes into svg.cache,
// and ensures downstream VectorGraphic caching is warmed up.
}
Example Usage
@override
void didChangeDependencies() {
super.didChangeDependencies();
// Properly warms up caches with the correct inherited SvgTheme and Locale
precacheSvg(SvgNetworkLoader('https://example.com/icon.svg'), context);
}
See #186479 RE adding MemoryBytesLoader
See #186480 RE adding PictureVectorGraphic
Description / Motivation
Developers attempting to precache vector graphics or SVGs to prevent route transition jank often run into cache misses and performance regressions.
There are two primary caching bottlenecks:
flutter_svg,svg.cachekeys include theSvgThemederived fromBuildContext(DefaultSvgTheme.of(context)). If a developer precaches early by callingloader.loadBytes(null), the cache key uses a default theme. When the widget is later rendered inside aMaterialApproute hierarchy with different inherited text styles or font sizes, the cache key mismatches, causingsvg.cacheto miss and re-fetch/re-parse everything on the fly.vector_graphics, the internal_livePictureCacheis reference-counted (count == 0when no widgets are actively mounted). Therefore, preloading bytes doesn't keep the decodedui.Picturealive across route transitions.Providing dedicated
precacheVectorGraphicandprecacheSvgfunctions (analogous to Flutter'sprecacheImage) would allow developers to correctly warm up both the byte cache and the live picture cache using the targetBuildContext.Proposed API
Add utility functions in both packages:
Example Usage
See #186479 RE adding
MemoryBytesLoaderSee #186480 RE adding
PictureVectorGraphic