flutter 参数类型'CollectionReference〈Object?>'不能分配给参数类型'Query〈Map〈String,dynamic>>',(文件)

xoshrz7s  于 2023-05-01  发布在  Flutter
关注(0)|答案(1)|浏览(151)


我正在写一个显示附近ATM机的应用程序。我想通过集合引用,我得到这个错误。

Stream nearbyATM() async* {
    GeoFirePoint point = geo.point(latitude: _position!.latitude, longitude: _position!.longitude);
    final CollectionReference atms = _firestore.collection("atms");
    double radius = 100;
    String field = "location";
    Stream<List<DocumentSnapshot>> stream = geo
                            .collection(collectionRef: atms)
                            .within(center: point, radius: radius, field: field);
  }

hrysbysz

hrysbysz1#

要在GeoFlutterFire包中使用.within()方法,需要将Query对象作为参数传递给**GeoFireCollectionRef。collection()**方法
你可以直接使用var query = _firestore.collection("atms").where(field, isLessThanOrEqualTo: point.distance, isGreaterThan: point.distance - radius);来使推理做它的事情,但如果我们也可以隐式地定义类型,如下所示:

Stream nearbyATM() async* {
  GeoFirePoint point = geo.point(latitude: _position!.latitude, longitude: _position!.longitude);
  double radius = 100;
  String field = "location";
// Let the Auto inference do it’s thing
  Query<Map<String, dynamic>> query =  _firestore.collection("atms").where(field, isLessThanOrEqualTo: point.distance, isGreaterThan: point.distance - radius);
// You can also 
  Stream<List<DocumentSnapshot>> stream = geo.collection(collectionRef: query).within(center: point, radius: radius, field: field);
}

我们可以将Firebase Query传递给geo.collection的collectionRef,而不仅仅是firestore的CollectionRef。
参考:geoflutterfire

相关问题