Query a collection to get documents close to the user location: radius (2km)

I have a collection called "Alerts" with a field "Location" (LatLng).

I'd like to query only the "Alerts" in a 2 kilometers radius.

I did a custom action but the package geoflutterfire is not compatible with the current version of flutterflow/flutter.

Do you have an idea how I can do that ?

Thank you very much

Here is my incompatible code:

import 'package:geolocator/geolocator.dart';
import 'package:geoflutterfire/geoflutterfire.dart';

Future<List<AlertsRecord>> getAlertNearBy(
  LatLng userLocation,
) async {
  final geo = Geoflutterfire();
  final FirebaseFirestore firestore = FirebaseFirestore.instance;
  final CollectionReference alertsCollection = firestore.collection('Alerts');

  final center = GeoFirePoint(userLocation.latitude, userLocation.longitude);
  final radius = 2.0; // 2 kilometers

  final alerts = await geo.collection(collectionRef: alertsCollection).within(
        center: center,
        radius: radius,
        field: 'location',
        strictMode: true,
      );

  return alerts.docs;
}
3
6 replies