loadAssets static method

Future<List<String>> loadAssets(
  1. List<String> assetPaths, {
  2. AssetBundle? assetBundle,
})

Loads a list of asset paths and returns the bundles assets as raw strings.

  • assetPaths: List of paths to the bundled assets to load.
  • {assetBundle}: Optional context to use. Defaults to rootBundle.
FutureBuilder<List<String>>(
  future: HighchartsHelpers.loadAssets(['assets/highcharts.js']),
  builder: (context, snapshot) {
    if (!snapshot.hasData) return const CircularProgressIndicator();
    return HighchartsChart(
      HighchartsOptions(),
      javaScriptModules: snapshot.data!
    );
  }
)

Implementation

static Future<List<String>> loadAssets(List<String> assetPaths,
    {AssetBundle? assetBundle}) async {
  final bundle = assetBundle ?? rootBundle;
  final loaded = <String>[];

  for (var path in assetPaths) {
    try {
      loaded.add(await bundle.loadString(path));
    } catch (error) {
      if (error is FlutterError &&
          error.message.contains('Unable to load asset')) {
        debugPrint('Asset not found: $path');
      } else {
        debugPrint('Error loading asset "$path": $error');
      }
    }
  }

  return loaded;
}