this is my first post here and for a student project, so I am very sorry if my format of questioning is incorrect. I created a minimal repo and run the build to recreate my error. The Goel is to create places including geopoints by flutter forms and save tehm in firebase as well as call GeoPoints from firebase and use these to automatically in flutter google maps.
Here is the link to the repo with the error message in readme: https://github.com/FREEW0RK/geopoint_test in this place.dart file: 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)};
with the JSON formatting here: 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]"]
},
in assets/images/places.json is the testfile which is loaded for initialization to firebase via firefoo. I tried multiple ways already, but dont get it to create a clean build or run including the initialisation test (included in places.dart same directory as main.dart)
this is the current error trying with @JsonKey insside freeze class.
[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>
Does anyone know how to handle custom datatypes like geopoint in flutter using freeze and firebase or can help me to get this to run somehow? Thank you very very much!!
I tried alreay the @JsonKey with custom _geoPointfromJson and _geoPointtoHson methods as well as hand coded classes and JsonCOnverter. Nothing cerates a clean build which also lets me test my json in checkinitialization correctly using the places.json. In firebase all in place collection is a geopoint with two doubles
I just want to run this somehow
Extract both static methods as top-level functions, that should work.
See this issue: https://github.com/rrousselGit/freezed/issues/115#issuecomment-603695583