doAsync<T> method

Future<T> doAsync<T>(
  1. Future<T> work(), {
  2. dynamic busyTaskKey,
})

Executes a long running task asynchronously.

Automatically sets the view model busy status.

busyTaskKey can be optionally set to help identify why the view model is busy. This can then be accessed by the UI to react differently depending on what the view model is doing.

Example:

Future<void> loadUsers() async {
   final users = await doAsync<User>(
     () async => await userService.getAll(),
     busyTaskKey: 'loadingUsers'
   );
}

Implementation

Future<T> doAsync<T>(Future<T> Function() work, {dynamic busyTaskKey}) async {
  setBusyStatus(isBusy: true, busyTaskKey: busyTaskKey);

  try {
    final result = await work();
    return result;
  } finally {
    setBusyStatus(isBusy: false, busyTaskKey: busyTaskKey);
  }
}