getLoggedInUser_async method

Future<User> getLoggedInUser_async({
  1. bool? ensureUserIDIsSet,
})

Retrieves the currently logged-in user asynchronously.

If no user is logged in, it defaults to returning the demo user. If ensureUserIDIsSet is true, it will ensure the user has an ID by fetching the user's ID if it's not set.

  • ensureUserIDIsSet: Whether to ensure the user has an ID. Defaults to false.

Returns a User object representing the logged-in user.

Implementation

Future<User> getLoggedInUser_async({bool? ensureUserIDIsSet}) async {
  if (_userIsLoggedIn) {
    // Get the current user or return the demo user.
    User user = _loggedInUser ?? User.getDemoUser();

    // Ensure the user ID is set if required.
    if (user.ID == 0 && (ensureUserIDIsSet ?? false)) {
      user = await dataHandler.getUserRepo().getUserWithID(user: user);
      _loggedInUser = user;
    }
    return user;
  } else {
    // If the user is not logged in, delay the function call until user login is complete.
    await Future.delayed(const Duration(milliseconds: 100));
    return await getLoggedInUser_async(
        ensureUserIDIsSet: ensureUserIDIsSet ?? false);
  }
}