I wrote this code with CodePilot aiming to return the 20 closest documents (nasoni) to the user location but it gives me the error - The getter 'latLng' isn't defined for the type 'NasoniRecord'. Try importing the library that defines 'latLng', correcting the name to the name of an existing getter, or defining a getter or field named 'latLng'. - any idea on how to fix it ?
To give some context the collection "nasoni" has only 1 LatLng field called "indirizzo"
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 '/backend/schema/structs/index.dart';
import '/backend/sqlite/sqlite_manager.dart';
import '/auth/firebase_auth/auth_util.dart';
List<NasoniRecord> closestNasonisToUserLocation(
List<NasoniRecord>? nasonis,
LatLng? userLocation,
) {
/// MODIFY CODE ONLY BELOW THIS LINE
// return 20 closest Nasonis to UserLocation
if (nasonis == null || userLocation == null) {
return [];
}
// Calculate distance between two latlng points using Haversine formula
double distanceBetweenLatLng(LatLng point1, LatLng point2) {
const int earthRadius = 6371; // in km
double lat1 = math.pi / 180 * point1.latitude;
double lat2 = math.pi / 180 * point2.latitude;
double lon1 = math.pi / 180 * point1.longitude;
double lon2 = math.pi / 180 * point2.longitude;
double dLat = lat2 - lat1;
double dLon = lon2 - lon1;
double a = math.sin(dLat / 2) * math.sin(dLat / 2) +
math.cos(lat1) *
math.cos(lat2) *
math.sin(dLon / 2) *
math.sin(dLon / 2);
double c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a));
double distance = earthRadius * c;
return distance;
}
// Sort nasonis by distance from user location
nasonis.sort((a, b) => distanceBetweenLatLng(a.latLng, userLocation)
.compareTo(distanceBetweenLatLng(b.latLng, userLocation)));
// Return the 20 closest nasonis
return nasonis.take(20).toList();
/// MODIFY CODE ONLY ABOVE THIS LINE
}