lottie-flutter icon indicating copy to clipboard operation
lottie-flutter copied to clipboard

Provide precache function for golden tests

Open MeneDev opened this issue 5 years ago • 2 comments

I tried to use a LottieBuilder as part of a golden test like this

Column(children: <Widget>[
      Icon(Icons.text_fields),
      Text('Test', style: TextStyle(fontFamily: 'Bangers')),
      Lottie.asset('assets/lottie/test.json', repeat: false),
      Image.asset('assets/images/test.png'),
])

Inside the widget test I try to wait for the LottieProvider to finish loading (LottieProvider.load().then(…)) but while I managed to render other images and fonts, I still fail to get an Image.

A precache function similar to widgets/precacheImage would be very helpful.

MeneDev avatar Aug 08 '20 12:08 MeneDev

Hi, Thanks for the feature request.

I've needed this feature too but it was a bit tricky to implement correctly so I put it aside for now. I may try again soon.

My current workaround is something like:

testWidgets('My golden test', (tester) async {
  await tester.runAsync(() => AssetLottie('asset/lottie/test.json').load());

  // Do actual golden test...
});

xvrh avatar Aug 15 '20 15:08 xvrh

Thanks for this excellent package!

I would also find precache functionality useful, but I'm not familiar enough with the code to know how to implement it. The package flutter_svg has this functionality, so perhaps ideas from there can be used. See https://pub.dev/documentation/flutter_svg/latest/svg/precachePicture.html https://stackoverflow.com/questions/62257156/how-to-use-precachepicture-in-flutter-svg

Example usage:

Future<void> main() async {
    await precachePicture(ExactAssetPicture(SvgPicture.svgStringDecoder, 'assets/images/image.svg'), null);
    runApp(YourApp());
}

olof-dev avatar Jan 02 '21 16:01 olof-dev