authorized static method

dynamic authorized(
  1. dynamic perform(), {
  2. required bool when(),
  3. dynamic unauthorized()?,
})

Perform an action only if the user is authorized to do so. Provide a perform function to execute if the user is authorized. Provide a when function to check if the user is authorized. Provide an unauthorized function to execute if the user is not authorized.

Implementation

static authorized(Function() perform,
    {required bool Function() when, Function()? unauthorized}) async {
  bool canPerform = when();
  if (!canPerform) {
    if (unauthorized != null) {
      await unauthorized();
    }
    return;
  }
  await perform();
}