accessing rootBundle in flutter unit test

1,214

Add this line as the first to be executed in your test and it will work and load the assets:

TestWidgetsFlutterBinding.ensureInitialized();

It will initialise everything before running your code and the assets will be accessible at that point.

Share:
1,214
AVEbrahimi
Author by

AVEbrahimi

Untying an impossibly tangled knot is actually possible. For every problem, there is a solution. And I am just one who is interested in untying and solving, that's it :)

Updated on December 15, 2022

Comments

  • AVEbrahimi
    AVEbrahimi over 1 year

    I am writing a unit test in my flutter app, seems I can't read json from assets in tests, but it works if I run app,

    I use code below to read json as string :

    var jsonString = await rootBundle.loadString("assets/student_list.json");
    

    How can I access jsons assets in tests?