distance filtering / geoflutterfire 2

has anyone tried to filter by distance using geoflutterfire2 and had success? i made this custom action and it saves with no errors but doesnt return any docs. i imported the proper dependency as well.

// Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/backend/schema/structs/index.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!

import 'package:geoflutterfire2/geoflutterfire2.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

Future<List<DocumentReference>> getEventsNearBy(
  LatLng userLocation,
  double maxDistanceMiles,
) async {
  final geo = GeoFlutterFire();

  final FirebaseFirestore firestore = FirebaseFirestore.instance;
  final CollectionReference eventsCollection =
      firestore.collection('userEvents');

  final center = GeoFirePoint(userLocation.latitude, userLocation.longitude);

  // Convert maxDistanceMiles to kilometers
  final maxDistanceKm = maxDistanceMiles * 1.60934;

  final eventsStream = geo.collection(collectionRef: eventsCollection).within(
        center: center,
        radius: maxDistanceKm,
        field: 'eventLatLng', // Use the field where you store LatLng data
        strictMode: true,
      );

  final List<DocumentReference> nearbyEventReferences = [];

  // Listen to the stream and collect results
  final subscription = eventsStream.listen((events) {
    for (final event in events) {
      nearbyEventReferences.add(eventsCollection.doc(event.id));
    }
  });

  // Wait for the subscription to complete
  await subscription.asFuture<void>();

  return nearbyEventReferences;
}
2
2 replies