getReportsNear method

Future<List<Report>> getReportsNear(
  1. GeoPoint location,
  2. double radiusKm
)

//////////// //////////// Gets reports near a specific location.

Implementation

// II.C - Geospatial Queries
///////////////
/// Gets reports near a specific location.
Future<List<Report>> getReportsNear(GeoPoint location, double radiusKm) async {
  try {
    final now = Timestamp.now();
    final collectionRef = _firestore.collection(_collectionPath);

    // Convert GeoPoint to RaliPosition first, then use it with GeoFirePoint
    final raliPosition = RaliPosition.fromFirebaseGeoPoint(location);

    // Query using GeoFlutterFire
    final stream = _geo.collection(collectionRef: collectionRef).within(
          center: GeoFirePoint(raliPosition.lat, raliPosition.lng),
          radius: radiusKm,
          field: 'geohash',
          strictMode: true,
        );

    final documents = await stream.first;
    final reports = documents
        .where((doc) {
          final data = doc.data() as Map<String, dynamic>?;
          if (data == null) return false;

          final Timestamp? expiresAt = data['expiresAt'] as Timestamp?;
          return expiresAt != null && expiresAt.compareTo(now) > 0;
        })
        .map((doc) => Report.fromMap(doc.data() as Map<String, dynamic>, doc.id))
        .toList();

    return reports;
  } catch (e) {
    throw Exception('Error getting nearby reports: $e');
  }
}