This is the code inside the builder method of the Streambuilder that causes the issue:
List<User> users = snapshot.data;
users.sort((user1, user2) => (user1.distanceInKm ?? 1000).compareTo(user2.distanceInKm ?? 1000));
If I use the following stream for the Streambuilder the above sorting works:
static Stream<List<User>> getUsersStreamWithDistance(
{@required User loggedInUser}) {
try {
var userSnapshots = _fireStore.collection('users').snapshots().map(
(snap) => snap.documents
.map((doc) => User.fromMap(map: doc.data))
.where((user) => user.email != loggedInUser.email)
.map((user) {
user.updateDistanceToOtherUser(otherUser: loggedInUser);
return user;
}).toList());
return userSnapshots;
} catch (e) {
print(e);
return null;
}
}
But not when I use the following stream, which is the one I need (ZipStream is from rxdart package):
static Stream<List<User>> getSpecifiedUsersStreamWithDistance(
{@required User loggedInUser, @required List<String> uids}) {
try {
List<Stream<User>> listOfStreams = [];
for (var uid in uids) {
Stream<User> streamToAdd = _fireStore
.collection('users')
.where('email', isEqualTo: uid)
.snapshots()
.map((snap) => snap.documents
.map((doc) => User.fromMap(map: doc.data))
.map((user) {
user.updateDistanceToOtherUser(otherUser: loggedInUser);
return user;
}).toList()[0]);
listOfStreams.add(streamToAdd);
}
Stream<List<User>> usersStream = ZipStream.list(listOfStreams);
return usersStream;
} catch (e) {
print(e);
return null;
}
}