_CastError (type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>' in type cast)

1,897

Solution 1

As posted in other question I was able to deserializing Firestore document by encode in to JSON string and back to JSON object before deserialization.

@Chiziaruhoma Ogbonda thanks for clarification, it helps me to think in other way rather directly tying to deserialize document.

Solution 2

Okay so i don't know if you know but then firestore sends you a Map not JSON. You're trying to use JSON Serializer. JSON is Map<String,dynamic> while firestore sends Map<dynamic,dynamic>. So you can't use parse it as json.

Check this out https://medium.com/@atul.sharma_94062/how-to-use-cloud-firestore-with-flutter-e6f9e8821b27

Solution 3

The solution is to use the anyMap and explicitToJson properties.

@JsonSerializable(explicitToJson: true, anyMap: true)
class Book {

}
Share:
1,897
jay
Author by

jay

Updated on December 15, 2022

Comments

  • jay
    jay over 1 year

    Im using Dart "json_serializable" package to deserialize below Firestore data structure in Flutter application.

    {
    googleBookId: jjl4BgAAQBAJ, 
    providers: [
        {providerId: 2FA9fULKLLf7VUPPFnFRnv}, 
        {providerId: 8UYTGUHY7UJS432FVBJRnv}
    ]
    }
    

    And below is the Model class to map:

    @JsonSerializable()
    class Book {
    
      String googleBookId;
      List<Provider> providers;
    
      Book(this.googleBookId,
      {List<Provider> providers})
      : providers = providers ?? <Provider>[];
    
      factory Book.fromJson(Map<String, dynamic> map) => _$BookFromJson(map);
    
      Map<String, dynamic> toJson() => _$BookToJson(this);
    }
    
    @JsonSerializable()
    class Provider {
    
      String providerId;
    
      Provider(this.providerId);
    
      factory Provider.fromJson(Map<String, dynamic> map) => _$ProviderFromJson(map);
    
      Map<String, dynamic> toJson() => _$ProviderToJson(this);
    }
    

    While deserializing I'm getting following error

    _CastError (type '_InternalLinkedHashMap' is not a subtype of type 'Map' in type cast)

    Is there any other library that I can use to deserialize document?