Hi everyone,
I am trying to create a custom function that returns list of documents based on 2 conditions:
1. First, "pujaName"(basically the service they wants for themself) selected by user on app matches the "PujaName" field (containing the name of the service) of each document contained in a collection.
Second, location field value in each document is within 5 km radius of user's location.
Below are the arguments defined and return value types:
Below is the 2nd attempted code that I used:
import 'dart:convert';
import 'dart:math' as math;
import 'package:geoflutterfire/geoflutterfire.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:intl/intl.dart';
import 'package:timeago/timeago.dart' as timeago;
import '/flutter_flow/lat_lng.dart';
import '/flutter_flow/place.dart';
import '/flutter_flow/uploaded_file.dart';
import '/flutter_flow/custom_functions.dart';
import '/backend/backend.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import '/auth/firebase_auth/auth_util.dart';
final geo = Geoflutterfire();
CollectionReference pujaQuotaCollection = FirebaseFirestore.instance.collection('pujaQuota');
List<PujaQuotaRecord>? newNearbyAds(
String pujaName,
LatLng userGeo,
LatLng location,
) {
GeoFirePoint center = geo.point(latitude: location.latitude, longitude: location.longitude);
Stream<List<DocumentSnapshot>> stream = geo.collection(collectionRef: pujaQuotaCollection)
.within(center: center, radius: 5, field: 'location', strictMode: true)
.where('pujaName', isEqualTo: pujaName)
.snapshots();
return stream.listen((list) {
List<PujaQuotaRecord> nearbyAds = list.map((doc) => PujaQuotaRecord.fromFirestore(doc)).toList();
return nearbyAds;
});
}Below is the error for the above code:
I also tried another code, below is the one, but got errors there as well:
import 'dart:convert';
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:intl/intl.dart';
import 'package:timeago/timeago.dart' as timeago;
import '/flutter_flow/lat_lng.dart';
import '/flutter_flow/place.dart';
import '/flutter_flow/uploaded_file.dart';
import '/flutter_flow/custom_functions.dart';
import '/backend/backend.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import '/auth/firebase_auth/auth_util.dart';
List<PujaQuotaRecord>? newNearbyAds(
LatLng userGeo,
String pujaName,
LatLng location,
) {
/// MODIFY CODE ONLY BELOW THIS LINE
// Retrieve documents where 'pujaName' matches 'PujaName' field and 'location' is within 5km of user.
final double radius = 5.0; // in kilometers
final double distance = distanceBetween(userGeo, location);
if (distance > radius) {
return null;
}
final Query<PujaQuotaRecord> pujaQuotaQuery = FirebaseFirestore.instance
.collection('puja_quota')
.where('pujaName', isEqualTo: pujaName);
final List<PujaQuotaRecord> nearbyAds = <PujaQuotaRecord>[];
pujaQuotaQuery.get().then((QuerySnapshot<PujaQuotaRecord> snapshot) {
for (final QueryDocumentSnapshot<PujaQuotaRecord> doc in snapshot.docs) {
final PujaQuotaRecord pujaQuotaRecord = doc.data();
final LatLng adLocation = LatLng(
pujaQuotaRecord.latitude,
pujaQuotaRecord.longitude,
);
final double adDistance = distanceBetween(userGeo, adLocation);
if (adDistance <= radius) {
nearbyAds.add(pujaQuotaRecord);
}
}
});
return nearbyAds;
/// MODIFY CODE ONLY ABOVE THIS LINE
}Here is the error when I test the code:
Can anyone please help me out with correcting any one of this code or maybe tell me some other solution altogether. Any help would be highly appreciated!