stvndelucis / firestore_api_parser

BSD 3-Clause "New" or "Revised" License
2 stars 2 forks source link

Can't create a Firestore Document using convertToFirestoreDocument output #9

Open boganiLuca opened 8 months ago

boganiLuca commented 8 months ago

Hello, I'm developing a pure Dart application that needs to interact with Google Firestore, and, boy, was I happy when I found your package! I was surrendering to the idea of manually write a conversion method to adapt the the Firestore JSON format, but luckily I found this gem.

In any case, I'm getting this stacktrace error from Firestore when I try to create a Document using Document.fromJson:

type '_Map<dynamic, Map<String, dynamic>>' is not a subtype of type 'Map<String, dynamic>' in type cast new MapValue.fromJson (package:googleapis/firestore/v1.dart:4961) new Value.fromJson (package:googleapis/firestore/v1.dart:5929) new Document.fromJson. (package:googleapis/firestore/v1.dart:3034) MapBase.map (dart:collection/maps.dart:82) new Document.fromJson (package:googleapis/firestore/v1.dart:3031) createNewRequest (package:voden_cloud_api/voden_cloud_api.dart:39) \<asynchronous suspension> RouterEntry.invoke. (package:shelf_router/src/router_entry.dart:107) \<asynchronous suspension> RouterEntry.invoke (package:shelf_router/src/router_entry.dart:104) \<asynchronous suspension> Router.call (package:shelf_router/src/router.dart:184) \<asynchronous suspension> handleRequest (package:shelf/shelf_io.dart:138) \<asynchronous suspension>

This is the piece of Dart code:

var reqString = await request.readAsString();

Map<String, dynamic> decoded = jsonDecode(reqString);
var uuid = Uuid().v1();
decoded.putIfAbsent("uuid", () => uuid);

final firestoreJsonDoc = FirestoreApiParser.convertToFirestoreDocument(json: decoded);

var document = Document.fromJson(firestoreJsonDoc);

The decoded map is like this, when printed out to console through jsonEncode of dart:convert:

{
   "errori":"",
   "additionalInfo":"gwe",
   "infoFarmacia":"Nome organizzazioneIndirizzo - PaeseTelefono - emailAltro",
   "analisi":{
      "tipoCampione":"Sangue capillare",
      "risultati":[
         {
            "flag":"0",
            "umis":"g/dL",
            "analita":"ALB",
            "range":"3.6-4.8",
            "value":"123,0"
         },
         {
            "flag":"0",
            "umis":"mg/dL",
            "analita":"UREA",
            "range":"15-48",
            "value":"12"
         },
         {
            "flag":"0",
            "umis":"mg/dL",
            "analita":"TB",
            "range":"0.3-1.0",
            "value":"0,5"
         }
      ],
      "dttm":"20240226T145334",
      "lotto":"",
      "isQC":false,
      "profilo":"General Chemistry Kit - Ins. manuale"
   },
   "uuid":"c34d24b0-c743-1ee8-b454-8bac812cc692"
}

The firestoreJsonDoc is looking like this, again printed through jsonEncode of dart:convert:

{
   "fields":{
      "errori":{
         "stringValue":""
      },
      "additionalInfo":{
         "stringValue":"gwe"
      },
      "infoFarmacia":{
         "stringValue":"Nome organizzazioneIndirizzo - PaeseTelefono - emailAltro"
      },
      "analisi":{
         "mapValue":{
            "fields":{
               "tipoCampione":{
                  "stringValue":"Sangue capillare"
               },
               "risultati":{
                  "arrayValue":{
                     "values":[
                        {
                           "mapValue":{
                              "fields":{
                                 "flag":{
                                    "stringValue":"0"
                                 },
                                 "umis":{
                                    "stringValue":"g/dL"
                                 },
                                 "analita":{
                                    "stringValue":"ALB"
                                 },
                                 "range":{
                                    "stringValue":"3.6-4.8"
                                 },
                                 "value":{
                                    "stringValue":"123,0"
                                 }
                              }
                           }
                        },
                        {
                           "mapValue":{
                              "fields":{
                                 "flag":{
                                    "stringValue":"0"
                                 },
                                 "umis":{
                                    "stringValue":"mg/dL"
                                 },
                                 "analita":{
                                    "stringValue":"UREA"
                                 },
                                 "range":{
                                    "stringValue":"15-48"
                                 },
                                 "value":{
                                    "stringValue":"12"
                                 }
                              }
                           }
                        },
                        {
                           "mapValue":{
                              "fields":{
                                 "flag":{
                                    "stringValue":"0"
                                 },
                                 "umis":{
                                    "stringValue":"mg/dL"
                                 },
                                 "analita":{
                                    "stringValue":"TB"
                                 },
                                 "range":{
                                    "stringValue":"0.3-1.0"
                                 },
                                 "value":{
                                    "stringValue":"0,5"
                                 }
                              }
                           }
                        }
                     ]
                  }
               },
               "dttm":{
                  "stringValue":"20240226T145334"
               },
               "lotto":{
                  "stringValue":""
               },
               "isQC":{
                  "booleanValue":false
               },
               "profilo":{
                  "stringValue":"General Chemistry Kit - Ins. manuale"
               }
            }
         }
      },
      "uuid":{
         "stringValue":"c34d24b0-c743-1ee8-b454-8bac812cc692"
      }
   }
}

Can someone give me any pointers on where the problem is? From what I can see, the "special" JSON format for Firestore seems correct, each "mapValue" has its own "fields", and the only array has its "arrayValue"-"values" pair, so I'm pretty stuck at this point

a-wallen commented 6 months ago

Use the code here: https://github.com/google/googleapis.dart/issues/92#issuecomment-651090736