flutter_svg icon indicating copy to clipboard operation
flutter_svg copied to clipboard

Using flutter_svg in tests

Open rgjurgens opened this issue 2 years ago • 2 comments

I'm using flutter_svg throughout our project and it works just fine. However, if I'm running the app in an integration test, the app crashes on assets that cannot be found. The error message is as follows, I'm using the latest flutter_svg version:

══╡ EXCEPTION CAUGHT BY SVG ╞═══════════════════════════════════════════════════════════════════════
The following assertion was thrown resolving a single-frame picture stream:
Unable to load asset: assets/buttons/close_button.svg

This is the code in my test:

import '../main.dart' as app;

void main() {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();

  group('end-to-end test', () {
    testWidgets('tap on the floating action button, verify counter',
        (tester) async {
      app.main();
      await tester.pumpAndSettle();

rgjurgens avatar Jul 21 '22 19:07 rgjurgens

Any thoughts, remarks? Do I need to provide more information?

rgjurgens avatar Jul 25 '22 08:07 rgjurgens

I had the same issue SvgPicture.network, and I solved with HttpOverrides.

In your case, you could mock the AssetBundle.

Another solution that I used in my code, is to convert the svg icons to a font pack. That way I can use them as Icon like I would do with a material icon, instead of Svg.

Momy93 avatar Jul 30 '22 14:07 Momy93

Realise this was a long time ago, but what folders do you have added to your pubspec.yaml? I saw this issue when I forgot to add the path to the containing folder, as well as the overall folder. ie

assets:
  - assets/
  - assets/buttons/

How the assets loaded was actually not consistent between running the app and running the tests. Adding the subfolders made it work for both.

HelenaSewell avatar Jan 26 '23 17:01 HelenaSewell