Flutter – Load assets for tests

Issue

Not sure if it’s a limitation or something, but below code does not load anything. I have some data driven behaviour that I’d like to test isolated.

class Loader

import 'dart:async';
import 'package:flutter/services.dart' show rootBundle;

class Loader {
  Future<String> load() async{
    return await rootBundle.loadString('assets/json/sketch.json');
  }
}

The test

testWidgets('Should parse load sketch.json', (WidgetTester tester) async {
    var loaderFuture = new Loader();

    Future<String> resultFuture = loaderFuture.load();

    resultFuture.then((value) => print(value))
        .catchError((error) => print(error));

    while(true){};
  });

Future does not return neither success nor error and hangs forever. I know
the while(true) locking up the test, but for now I just wanted to see sketch.json printed

Asset location

enter image description here

Solution

See the documentation of DefaultAssetBundle it describes using it and a AssetBundle to provide your own assets.

Answered By – Simon

Answer Checked By – Candace Johnson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.