google / built_value.dart

Immutable value types, enum classes, and serialization.
https://pub.dev/packages/built_value
BSD 3-Clause "New" or "Revised" License
870 stars 184 forks source link

Firestore DocumentReference deserilazition problem. (version '>=5.5.5 <7.0.0') #824

Closed gurolcay closed 4 years ago

gurolcay commented 4 years ago

I am using build_value lib is version '>=5.5.5 <7.0.0'

I have a problem which is when I deserialize my model, I can't get correctly. Because the model has a list of DocumentReference.

My model:

abstract class User implements Built<User, UserBuilder> {
  static Serializer<User> get serializer => _$userSerializer;

  String get userID;

  String get email;

  bool get acceptTerms;

  BuiltList<DocumentReference> get lessonsReference;

  @nullable
  BuiltList<DocumentReference> get groupsReference;

  @nullable
  BuiltList<DocumentReference> get studiesReference;

  User._();

  factory User([void Function(UserBuilder) updates]) = _$User;

  String toJson() {
    return json
        .encode(standardSerializers.serializeWith(User.serializer, this));
  }

  static User fromJson(String jsonString) {
    return standardSerializers.deserializeWith(
        User.serializer, json.decode(jsonString));
  }
}

When I want to get one of user from fırestore, i get this data:

{$: User, acceptTerms: true, groups: [Instance of 'DocumentReference'], studies: [Instance of 'DocumentReference', Instance of 'DocumentReference', Instance of 'DocumentReference', Instance of 'DocumentReference', Instance of 'DocumentReference', Instance of 'DocumentReference'], userID: 349407149, email: test@test.com, lessons: [Instance of 'DocumentReference', Instance of 'DocumentReference', Instance of 'DocumentReference', Instance of 'DocumentReference', Instance of 'DocumentReference']}

After deserialization, I got

User {
   userID=349407149,
   email=mgurolcay@gmail.com,
   acceptTerms=true,
   lessonsReference=[],
}

What can I do to fix this?

davidmorgan commented 4 years ago

The problem is that Firestore only partially serializes to JSON--as you've noticed, it leaves some classes like DocumentReference as the originals.

You can fix this with a custom serializer that just passes through the value, as described here for GeoPoint:

https://github.com/google/built_value.dart/issues/417#issuecomment-391661750