Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Serialization/Deserializaton with Map<String, dynamic> #887

Closed
JRamos29 opened this issue Jun 30, 2020 · 4 comments
Closed

Question: Serialization/Deserializaton with Map<String, dynamic> #887

JRamos29 opened this issue Jun 30, 2020 · 4 comments

Comments

@JRamos29
Copy link

JRamos29 commented Jun 30, 2020

Hi,
i'm trying to use built_value, but i can't figure it out how to make the methods toJson() and fromJson() transforms and receives a Map<String, dynamic> instead of String. I used a snippet to create my classes, and don't know what changes i need to make to achieve the serialization with Map<String, dynamic>. I need to use with Firestore and the method setData only accepts map. This is my current serializer:

final Serializers serializers =
    (_$serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build();

And here are the methods:

 String toJson() {
    return json.encode(serializers.serializeWith(Comment.serializer, this));
  }

  static Comment fromJson(String jsonString) {
    return serializers.deserializeWith(
        Comment.serializer, json.decode(jsonString));
  }
@JRamos29 JRamos29 changed the title Question: Serialization/Deserializaton with Map<String, dynamyc> Question: Serialization/Deserializaton with Map<String, dynamic> Jun 30, 2020
@JRamos29 JRamos29 closed this as completed Jul 4, 2020
@PalakSDarji
Copy link

Hey I am facing the same situation. Firebase APIs returns Map with list of key and value. BuiltValue cant parse it. The error returned is : Unhandled Exception: type 'BuiltMapSerializer' is not a subtype of type 'Serializer<BuiltMap<String, BuiltProduct>>'. Any solution you got?

@JRamos29
Copy link
Author

Hey I am facing the same situation. Firebase APIs returns Map with list of key and value. BuiltValue cant parse it. The error returned is : Unhandled Exception: type 'BuiltMapSerializer' is not a subtype of type 'Serializer<BuiltMap<String, BuiltProduct>>'. Any solution you got?

I asked at stackoverflow and received this answer. Hope it helps.

@binajmen
Copy link

Hi @JRamos29,

Did you succeed to implement the solution ?

I tried the following without success:

  Map<String, dynamic> toFirestore() {
    return serializers.serializeWith(Place.serializer, this);
  }

  static Place fromFirestore(Map<String, dynamic> json) {
    return serializers.deserializeWith(Place.serializer, json);
  }

A value of type 'Object?' can't be returned from the method 'toFirestore' because it has a return type of 'Map<String, dynamic>'.dartreturn_of_invalid_type

@JRamos29
Copy link
Author

Hi @binajmen.

The only possible solution I have is the one linked above.
And I stopped working on the project far ago, so I can't tell how was solved it either. Sorry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants