7

I want to reference a category document in my post document in firebase. This is my data class, I'm also using freezed and json_serializer:

    part 'post_dto.freezed.dart';
    part 'post_dto.g.dart';
    part 'category_dto.freezed.dart';
    part 'category_dto.g.dart';
    
    @freezed
    abstract class PostDTO with _$PostDTO {
      const PostDTO._();
    
      const factory PostDTO({
        @JsonKey(ignore: true) String? id,
        required String title,
        required String description,
        @DocumentReferenceConveter() DocumentReference? categoryReference,
      }) = _PostDTO;
    
      factory PostDTO.fromJson(Map json) =>
          _$PostDTOFromJson(json);
    
      factory PostDTO.fromFireStore(DocumentSnapshot document) {
        Map data = document.data() as Map;
        return PostDTO.fromJson(data).copyWith(id: document.id);
      }
    }
    
    @freezed
    abstract class CategoryDTO with _$CategoryDTO {
      const CategoryDTO._();
    
      const factory CategoryDTO({
        required String icon,
        required String name,
      }) = _CategoryDTO;
    
     factory CategoryDTO.fromFireStore(DocumentSnapshot document) {
        Map data = document.data() as Map;
        return CategoryDTO.fromJson(data);
      }
    
      factory CategoryDTO.fromJson(Map json) =>
          _$CategoryDTOFromJson(json);
    }

When I run build_runner I got this error:

    [SEVERE] json_serializable:json_serializable on lib/infrastructure/post/post_dto.dart:
    
    Could not generate `fromJson` code for `categoryReference`.
    To support the type `DocumentReference` 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:UPLFY/infrastructure/post/post_dto.freezed.dart:373:41
        ╷
    373 │   final DocumentReference? categoryReference;
        │                                         ^^^^^^^^^^^^^^^^^
        ╵
    [INFO] Running build completed, took 2.5s
    
    [INFO] Caching finalized dependency graph...
    [INFO] Caching finalized dependency graph completed, took 44ms
    
    [SEVERE] Failed after 2.5s

So tried using the JsonConverter but I'm not sure how to convert the json object to a DocumentReference...

    class DocumentReferenceConveter
        implements JsonConverter, Object> {
      const DocumentReferenceConveter();
    
      @override
      DocumentReference fromJson(Object json) {
        return //TODO: Convert json to DocumentReference
      }
    
      @override
      Object toJson(DocumentReference documentReference) =>
          documentReference;
    }

2 Answers 2

2

I was able to put together my solution from the research I found online and so far came up with this.

class DocumentReferenceJsonConverter
    implements JsonConverter<DocumentReference?, Object?> {
  const DocumentReferenceJsonConverter();

  @override
  DocumentReference? fromJson(Object? json) {
    if (json != null) {
      // It should work if you just cast it
      return json as DocumentReference;
    }
  }

  @override
  Object? toJson(DocumentReference? documentReference) => documentReference;
}
Sign up to request clarification or add additional context in comments.

Comments

0

I have been investigating and I found there is an issue related to some versions of the analyzer package. I leave it here in case it could be useful for someone in the community (if you use the '0.39.15' or '0.39.16' versions this could be the cause). If that is the case, you can set the override for now inside your pubspec.yaml:

dependency_overrides:
  analyzer: '0.39.14'

Also, you should clear all of the caches after:

flutter clean
flutter pub cache repair
flutter pub run build_runner clean

1 Comment

Thanks for the reply. But I got it working now... The problem was that I declared DocumentReference as a nullable type but in my converter, it was not. So I just added a "?" and it seems to be working now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.