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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With