getAssets method

String? getAssets(
  1. String path,
  2. void setStateCallback(),
  3. void errorCallback(
    1. dynamic error
    )
)

(en)Asynchronously retrieves the layout present at the given path and calls back when complete. If you specify an asset that has already been acquired, the contents will be returned as is and no callback will occur. If you specify an asset that has not yet been acquired, null will be returned, and resource acquisition will start after the current rendering is complete. Asset registration in pubspec.yaml is mandatory.

(ja) 指定されたパスに存在するレイアウトを非同期で取得し、完了したらコールバックします。 既に取得済みのアセットを指定した場合はそのまま内容が返却され、コールバックは発生しません。 まだ取得されていないアセットを指定した場合はnullが返却され、現在のレンダリングの完了後のタイミングでリソースの取得が開始されます。 pubspec.yamlにアセットの登録が必須です。

  • path : 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.

Implementation

String? getAssets(String path, void Function() setStateCallback,
    void Function(dynamic error) errorCallback) {
  if (_buffLayouts.containsKey(path)) {
    return _buffLayouts[path];
  } else {
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      try {
        _buffLayouts[path] = await rootBundle.loadString(path, cache: false);
        WidgetsBinding.instance.addPostFrameCallback((_) {
          setStateCallback();
        });
      } catch (e) {
        WidgetsBinding.instance.addPostFrameCallback((_) {
          errorCallback(e);
        });
      }
    });
    return null;
  }
}