1

I want to read a file from an asset during widget build with help of rootBundle but when I tried to call rootBundle.loadString('assets/myJsonAsset.json') like below :

testWidgets('load assets', (WidgetTester tester) async {
      final result = await rootBundle.loadString('assets/myJsonAsset.json');
      //above loadString method never return any result
      expect(result, isA<String>());
    });

then it causes the error : enter image description here

Unable to load asset: assets/myJsonAsset.json

And the rootBundle.loadString('assets/myJsonAsset.json') never return any result.

Taleb
  • 1,944
  • 2
  • 11
  • 36

1 Answers1

1

Try to add this line to be executed before your tests:

TestWidgetsFlutterBinding.ensureInitialized();

This function will make your assets available for tests

  • I also tried this but problem still exist, I think we need to find a way to mock the rootBundle service – Taleb Apr 27 '22 at 08:13