getUser method

Future<User?> getUser({
  1. bool refresh = false,
})

Gets the current user with automatic cache refresh.

Returns null if not logged in. Returns cached data if fresh (within TTL), fetches full profile from network if stale or missing. Falls back to stale cached data when offline (network errors are absorbed).

The returned user may have partial data (User.fetchedAt is null) if only login has been performed. Full profile data is fetched automatically when User.fetchedAt is null or stale.

Set refresh to true to bypass TTL and always refetch (for pull-to-refresh).

Implementation

Future<User?> getUser({bool refresh = false}) async {
  final user = await _database.select(_database.users).getSingleOrNull();
  if (user == null) return null; // Not logged in, can't fetch

  try {
    return await fetchWithTtl<User>(
      cached: user,
      getFetchedAt: (u) => u.fetchedAt,
      fetchFromNetwork: _fetchUserFromNetwork,
      refresh: refresh,
    );
  } on DioException {
    return user;
  }
}