awaitData method

Future<void> awaitData({
  1. String name = 'default',
  2. required Function perform,
  3. bool shouldSetStateBefore = true,
  4. bool shouldSetStateAfter = true,
})

Use the awaitData method when initial fetching data for a widget. E.g. When your page first loads and you want to populate your widgets with data.

init() async { awaitData('home', perform: () async { ... await fetchApiData(); }); }

... in your widget Text( isLoading('home') ? 'YES Loading' : 'Loading Finished').

Implementation

Future<void> awaitData({
  String name = 'default',
  required Function perform,
  bool shouldSetStateBefore = true,
  bool shouldSetStateAfter = true,
}) async {
  _updateLoadingState(
    shouldSetState: shouldSetStateBefore,
    name: name,
    value: true,
  );
  overrideLoading = true;
  try {
    await perform();
  } on Exception catch (e) {
    NyLogger.error(e.toString());
  }
  if (widget is NyStatefulWidget &&
      (widget as NyStatefulWidget).controller.routeGuards.isEmpty) {
    hasInitComplete = true;
  }
  _updateLoadingState(
    shouldSetState: shouldSetStateAfter,
    name: name,
    value: false,
  );
}