getMultiAssets method

List<String>? getMultiAssets(
  1. List<String> paths,
  2. void setStateCallback(),
  3. void errorCallback(
    1. dynamic error
    )
)

The multi-simultaneous version of getAssets. Asset registration in pubspec.yaml is mandatory.

  • paths : The layout path in assets directory. e.g. assets/layout/en/top/base.spwml
  • setStateCallback : Pass the setState of the parent widget in the callback. If you're using another state management, let the screen refresh. The callback will be fired after the current rendering is finished.
  • errorCallback : The callback in case loading fails. The callback will be fired after the current rendering is finished. This is fired only once when an asset fails to load. And the asset loading will stop at that point.

Implementation

List<String>? getMultiAssets(
    List<String> paths,
    void Function() setStateCallback,
    void Function(dynamic error) errorCallback) {
  bool isAllContained = true;
  for (String i in paths) {
    if (!_buffLayouts.containsKey(i)) {
      isAllContained = false;
      break;
    }
  }
  if (isAllContained) {
    List<String> r = [];
    for (String i in paths) {
      r.add(_buffLayouts[i]!);
    }
    return r;
  } else {
    // 足りない分をロードした後でコールバックする。
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      try {
        for (String i in paths) {
          if (!_buffLayouts.containsKey(i)) {
            _buffLayouts[i] = await rootBundle.loadString(i, cache: false);
          }
        }
        WidgetsBinding.instance.addPostFrameCallback((_) {
          setStateCallback();
        });
      } catch (e) {
        WidgetsBinding.instance.addPostFrameCallback((_) {
          errorCallback(e);
        });
      }
    });
    return null;
  }
}