getUser method

Future<RaliUser?> getUser(
  1. String uid
)

Gets a user by their UID with caching support

Implementation

Future<RaliUser?> getUser(String uid) async {
  try {
    // Check cache first for offline support
    if (_userCache.containsKey(uid)) {
      return _userCache[uid];
    }

    final docSnapshot = await _firestore.collection(_collectionPath).doc(uid).get();

    if (docSnapshot.exists) {
      final user = RaliUser.fromMap(docSnapshot.data()!, uid);
      // Update cache
      _userCache[uid] = user;
      return user;
    }
    return null;
  } catch (e) {
    print('Error getting user: $e');

    // Handle specific Firebase errors
    if (e is FirebaseException) {
      if (e.code == 'unavailable' && _userCache.containsKey(uid)) {
        // Return cached data if network is unavailable
        return _userCache[uid];
      }
    }

    throw RALIException('Failed to retrieve user profile', cause: e);
  }
}