hwe*_*ang 0 firebase flutter google-cloud-firestore
我正在尝试使用以下包按距离过滤我的 Firestore 集合。
https://github.com/fluttercommunity/firestore_helpers
参考他们的示例,他们使用 jaguar_serializer 将从 Firestore 接收到的数据转换为 Location 类。我已经在我的应用程序中实现了他们的 getDataInArea 函数,但我目前卡住了,因为我使用的是 json_serializable。
我环顾四周,发现如果在以下链接中使用 built_value,则有关于如何处理 GeoPoint 的建议。
https://github.com/google/built_value.dart/issues/417#issuecomment-391661750
所以我想知道有没有办法实现类似于 jaguar_serializer 和 built_value 中显示的内容,但使用 json_serializable 包。
我猜测一种方法是使用,@JsonKey(fromJson: , toJson: )但我不太确定如何去做。
小智 8
从 Firestore 返回的类型是 GeoPoint,您可以使用 JsonConverter 传递它:
class GeoPointConverter
implements JsonConverter<GeoPoint, GeoPoint> {
const GeoPointConverter();
@override
GeoPoint fromJson(GeoPoint geoPoint) {
return geoPoint;
}
@override
GeoPoint toJson(GeoPoint geoPoint) =>
geoPoint;
}
Run Code Online (Sandbox Code Playgroud)
之后,注释您的参数:
@GeoPointConverter() required GeoPoint location
经过更多的挖掘并试图了解事情是如何运作的。通过在课堂上写下以下内容,我设法解决了我的问题。
@JsonKey(fromJson: _fromJsonGeoPoint, toJson: _toJsonGeoPoint)
GeoPoint location;
static GeoPoint _fromJsonGeoPoint(GeoPoint geoPoint) {
return geoPoint;
}
static GeoPoint _toJsonGeoPoint(GeoPoint geoPoint) {
return geoPoint;
}
Run Code Online (Sandbox Code Playgroud)
我想这个想法只是不对 GeoPoint 对象进行任何更改。无论如何,很想听到这是正确的方法,或者有更好的方法来做到这一点。干杯!
| 归档时间: |
|
| 查看次数: |
715 次 |
| 最近记录: |