getResource method

String? getResource(
  1. String url,
  2. Future<String> getDataFunction(
    1. String url
    ),
  3. void setStateCallback(),
  4. void errorCallback(
    1. dynamic error
    ),
)

(en)Register with this class and call back when the resource acquisition is complete. If you specify a URL that has already been acquired, the content will be returned as is and no callback will occur. If you specify a URL that has not yet been acquired, null will be returned, and resource acquisition will start after the current rendering is completed.

(ja) リソースの取得が完了したらこのクラスに登録してコールバックします。 既に取得済みのURLを指定した場合はそのまま内容が返却され、コールバックは発生しません。 まだ取得されていないURLを指定した場合はnullが返却され、現在のレンダリングの完了後のタイミングでリソースの取得が開始されます。

  • url : The layout url.
  • getDataFunction : Pass the function that actually gets the resource via https and returns the result.
  • 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? getResource(
    String url,
    Future<String> Function(String url) getDataFunction,
    void Function() setStateCallback,
    void Function(dynamic error) errorCallback) {
  if (_buffLayouts.containsKey(url)) {
    return _buffLayouts[url];
  } else {
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      try {
        _buffLayouts[url] = await getDataFunction(url);
        WidgetsBinding.instance.addPostFrameCallback((_) {
          setStateCallback();
        });
      } catch (e) {
        WidgetsBinding.instance.addPostFrameCallback((_) {
          errorCallback(e);
        });
      }
    });
    return null;
  }
}