Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing rootBundle in flutter unit test

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?

like image 422
AVEbrahimi Avatar asked Nov 25 '19 08:11

AVEbrahimi


1 Answers

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.

like image 128
silvio.pereira Avatar answered Nov 20 '22 21:11

silvio.pereira