getNotifications method

Future<List<UserNotificationModel>> getNotifications({
  1. int limit = 50,
  2. DocumentSnapshot<Object?>? startAt,
  3. String? orderBy,
  4. bool descending = false,
  5. required String userId,
})

Implementation

Future<List<UserNotificationModel>> getNotifications({
  int limit = 50,
  DocumentSnapshot? startAt,
  String? orderBy,
  bool descending = false,
  required String userId,
}) async {
  try {
    final usersRef = _db.collection('Users');
    final userDocRef = usersRef.doc(userId);
    final userNotificationsRef = userDocRef.collection('UserNotifications');
    var query = userNotificationsRef.limit(limit);
    if (startAt is DocumentSnapshot) {
      query = query.startAtDocument(startAt);
    }
    if (orderBy is String) {
      query = query.orderBy(orderBy, descending: descending);
    }
    final snapshot = await query.get();

    if (snapshot.docs.isEmpty) {
      return [];
    }
    final toParse = snapshot.docs.map((e) async {
      final user = await UserNotificationModel.fromDocumentSnapshot(e);

      return user;
    }).toList();

    final parsed = await Future.wait(toParse);
    return parsed;
  } catch (err) {
    if (err is FirebaseException) {
      throw UserNotificationsDataServiceException(
        message: err.message,
        code: RdevCode.Internal,
        stackTrace: err.stackTrace,
      );
    }
    throw UserNotificationsDataServiceException(message: err.toString());
  }
}