Click here for the previous version well solved question by me and others
I want to sort the items in the model which represents the firebase documents of particular collections.Also this model contains the document Ids of the users(which will be sorted according to the distance) of the collection "users" and i will bring those documents only from "users" collection. I have calculated the distances from the current-user to the other users and i want bring the nearest users for this user. I have tried following code:
ScreenShot: [In the pink box area, whole calculation is done and assigned and documents brought from firebase]
[]2
Got Error:
Got this error: "Unhandled Exception: PlatformException(error, Invalid Query. 'in' filters cannot contain 'null' in the value array., null)" –
Detailed Code:
Temp model:
class certainrangeusers {
final String id;
final double away;
final GeoPoint userpoint;
certainrangeusers({this.id, this.away, this.userpoint});
}
Variables:
List<certainrangeusers> info= [];
code:
getUsers() async
{
double radius = 0.3;
String field = "GeoPosition";
print(currentUser.point.longitude);
GeoFirePoint center = geo.point(latitude: currentUser.point.latitude,
longitude: currentUser.point.longitude);
var collectionRef = Firestore.instance.collection('user_locations').document(currentUser.pincode)
.collection('Users_comp lete_address_and_geopoint');
this.stream = geo.collection(collectionRef: collectionRef)
.within(center: center, radius: radius, field: field, strictMode: false);
Future<List<certainrangeusers>> users1 = stream.first.then((documents) => documents.map((doc) =>
certainrangeusers(
id: doc['userPhone'],
userpoint : doc['GeoPosition']['geopoint'],
)
).toList());
users1.then((val) async{
for(var value in val){
info.add(certainrangeusers(
//away: await Geolocator().distanceBetween(currentUser.point.latitude,currentUser.point.longitude, value.userpoint.latitude, value.userpoint.longitude),
away: center.distance(lat: value.userpoint.latitude,lng: value.userpoint.longitude),
));
info.sort((a,b) => a.away.compareTo(b.away));
}
List ids = [];
for(var value in info){
ids.add(value.id);
}
QuerySnapshot snapshot = await Firestore.instance.collection('users').where('id', whereIn: ids).getDocuments();
List<User> users = snapshot.documents.map((doc) => User.fromDocument(doc)).toList();
setState(() {
this.users = users;
});
});
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…