如何使用 Freezed 和 Firebase 在 Flutter 中从 Geopoints 或自定义数据类型 fromJSON/toJSON (总是构建错误)

问题描述 投票:0回答:1

这是我在这里发表的第一篇文章,也是一个学生项目,所以如果我的提问格式不正确,我感到非常抱歉。我创建了一个最小的存储库并运行构建以重新创建我的错误。 Goel 是通过 flutter 表单创建包括地理点的地方,并将 tehm 保存在 firebase 中,以及从 firebase 调用 GeoPoints 并在 flutter google 地图中自动使用它们。

以下是存储库的链接,其中包含自述文件中的错误消息: https://github.com/FREEW0RK/geopoint_test在这个place.dart文件中: https://github.com/FREEW0RK/geopoint_test/blob/main/geopoint_test/lib/place.dart

import 'dart:convert';

import 'package:flutter/services.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

part 'place.freezed.dart';
part 'place.g.dart';

/// Garden Document.
/// You must tell Firestore to use the 'id' field as the documentID
@freezed
@JsonSerializable(explicitToJson: true)
class Place with _$Place {
  const factory Place({
    required String id,
    required String name,
    required String description,
    @JsonKey(
    fromJson: _geoPointFromJson,
    toJson: _geoPointToJson,)
    required GeoPoint location, 
    required String placeType,
    required String imagePath,
    required String ownerID,
    //required String chapterID,
    required String lastUpdate,
    @Default([]) List<String> editorIDs,
    @Default([]) List<String> visitorIDs,
  }) = _Place;

  factory Place.fromJson(Map<String, dynamic> json) => _$PlaceFromJson(json);

  static GeoPoint _geoPointFromJson(Map<String, dynamic> json) =>
        const GeoPointConverter().fromJson(json['location']);

    static Map<String, dynamic> _geoPointToJson(GeoPoint geoPoint) =>
        {'location': const GeoPointConverter().toJson(geoPoint)};

此处采用 JSON 格式: https://github.com/FREEW0RK/geopoint_test/blob/main/geopoint_test/assets/images/places.json

    [
  {
      "id": "places-001",
      "name": "University of Hawaii at Manoa (UHM)",
      "description": "Outdoor Workplaces in front of Natural Science Building with shed and electricity",
      "placeType": "public",
      "imagePath": "assets/images/diamond.png",
      "location": {
        "latitude": 21.3,
        "longitude": -157.81
      },
      "lastUpdate": "11/15/22",
      "ownerID": "[email protected]",
      "editorIDs": ["[email protected]"],
      "visitorIDs": ["[email protected]", "[email protected]"]
  },

在 asset/images/places.json 中是通过 firefoo 加载用于初始化 firebase 的测试文件。我已经尝试了多种方法,但没有让它创建一个干净的构建或运行,包括初始化测试(包含在与 main.dart 相同的目录中)

这是在冻结类中尝试使用 @JsonKey 时出现的当前错误。


[SEVERE] json_serializable on lib/place.dart:

Could not generate `fromJson` code for `location`.
To support the type `GeoPoint` you can:
* Use `JsonConverter`
  https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonConverter-class.html* Use `JsonKey` fields `fromJson` and `toJson`
  https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/fromJson.html
  https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/toJson.html
package:geopoint_test/place.freezed.dart:27:16
   ╷
27 │   GeoPoint get location => throw _privateConstructorUsedError;
   │                ^^^^^^^^
   ╵
[INFO] Running build completed, took 1m 18s
[INFO] Caching finalized dependency graph completed, took 328ms
[SEVERE] Failed after 1m 19s
PS D:\APPDEV\geopoint_test\geopoint_test> 

有谁知道如何使用 freeze 和 firebase 处理 flutter 中的自定义数据类型(例如 geopoint),或者可以帮助我让它以某种方式运行?非常非常感谢!!

我尝试了 @JsonKey 和自定义 _geoPointfromJson 和 _geoPointtoHson 方法以及手工编码的类和 JsonCOnverter。没有任何东西可以保证干净的构建,这也让我可以使用places.json在检查初始化中正确测试我的json。在 firebase 中,所有就地集合是一个带有两个双精度的地理点

我只是想以某种方式运行它

flutter firebase freeze json-serialization geopoints
1个回答
0
投票

将两个静态方法提取为顶级函数,这应该可行。

@freezed
class Place with _$Place {
  // ...
}

GeoPoint _geoPointFromJson(Map<String, dynamic> json) =>
    const GeoPointConverter().fromJson(json['location']);

Map<String, dynamic> _geoPointToJson(GeoPoint geoPoint) =>
    {'location': const GeoPointConverter().toJson(geoPoint)};

查看此问题:https://github.com/rrousselGit/freezed/issues/115#issuecomment-603695583

© www.soinside.com 2019 - 2024. All rights reserved.