Flutter_svg: [Suggestion] Add a precache function

Created on 1 Mar 2019  路  3Comments  路  Source: dnfield/flutter_svg

In my app, I need to quickly display SVG pictures. As I have a loading screen, it would be great to be able to load my pictures using a precacheImage function (a bit like in flutter images function).

Something like :

precacheImage(SvgPicture.asset(
      'asset.svg'
      width: 512,
      fit: BoxFit.cover,
      semanticsLabel: 'My picture',
));

Which returns a Future<void> for when it's loaded.

Most helpful comment

Is there any example about how to use the precachePicture within a StatefulWidget?
I cannot load the Svg string from the assets, within the initState, as it cannot be an async function. Should I use didChangeDependencies?
Some pointers about the steps to follow would be really helpful.
Thanks!

All 3 comments

The problem is the return value of SvgPicture.asset is a Widget. I think this would actually take the PictureProvider, which could make more sense. Something like

precachePicture(ExactAssetPicture(
  svgStringDecoder,
  assetName,
   ...,
));

shouldn't be too hard to add though, and then if you used that asset it would be in the cache.

Your pull request is exactly what I was searching for !

Is there any example about how to use the precachePicture within a StatefulWidget?
I cannot load the Svg string from the assets, within the initState, as it cannot be an async function. Should I use didChangeDependencies?
Some pointers about the steps to follow would be really helpful.
Thanks!

Was this page helpful?
0 / 5 - 0 ratings