rrousselGit / freezed

Code generation for immutable classes that has a simple syntax/API without compromising on the features.
https://pub.dev/packages/freezed
1.93k stars 237 forks source link

GeoPoint won't generate fromJson code #345

Closed luqmanhakem closed 3 years ago

luqmanhakem commented 3 years ago

Describe the bug Hello. I tried to run flutter pub run build_runner build --delete-conflicting-outputs for this below code and this error occur.

Is this a bug or am I'm doing it wrong. Also, couldn't manage to find any open issue. Thanks in advance.

[SEVERE] json_serializable:json_serializable on lib/modules/splash/models/service/service.dart:

Could not generate `fromJson` code for `geopoint`.
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:sook/modules/splash/models/service/service.freezed.dart:415:18
    ╷
415 │   final GeoPoint geopoint;
    │                  ^^^^^^^^
    ╵
[INFO] Running build completed, took 6.2s

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 48ms

[SEVERE] Failed after 6.2s
pub finished with exit code 1

To Reproduce

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:sook/modules/splash/models/models.dart';

part 'service.g.dart';
part 'service.freezed.dart';

@freezed
abstract class Service with _$Service {
  const factory Service({
    @required String submittedAt,
    @required String approvedAt,
    @required String status,
    @required String name,
    @required String category,
    @required String subcategory,
    @required String description,
    @required String startPrice,
    @required String endPrice,
    @required String avgRating,
    @required String country,
    @required String state,
    @required String geohash,
    @required GeoPoint geopoint,
    @required List<String> images,
    @required Map<String, Day> availability,
    @required List<Review> reviewList
  }) = _Service;

  factory Service.fromJson(Map<String, dynamic> json) =>
      _$ServiceFromJson(json);
}

Expected behavior

smiLLe commented 3 years ago
 Could not generate `fromJson` code for `geopoint`.
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

scroll down until you see MyResponseConverter

luqmanhakem commented 3 years ago
 Could not generate `fromJson` code for `geopoint`.
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

scroll down until you see MyResponseConverter

Thank you! Able to generate .g.dart file. Haven't test this but will closed the issue as for now.

Kudos to you!

giorgio79 commented 2 years ago

so did you write a custom converter for geoPoint? Can you share the code? :)

PS Nevermind :) https://stackoverflow.com/questions/61707320/how-to-serialize-geopoint-using-dart-json-serializable-package

rrousselGit commented 2 years ago

Geopoints are natively supported by the ODM now. Just make sure you follow the docs and correctly setup the list of "JsonConverters" to include the built-in ones.