Error while sending List data to cloud firestore using REST api in flutter

721

Try writing your facility array value like this:

"facility": {
  "arrayValue": {
    "values": [
      {
        "stringValue": "lunch"
      },
      {
        "stringValue": "dinner"
      }
    ]
  }
}

The "values" part comes from the references for Value and arrayValue.

Share:
721
Avinash Mawle
Author by

Avinash Mawle

Updated on December 21, 2022

Comments

  • Avinash Mawle
    Avinash Mawle over 1 year

    I'm not able to send List of string to cloud firestore using REST api in flutter.

    //###### here is my code ##########
    
    Future<bool> addVisit(Visit visit) async { //function
    try {
    var response = await http.post( //post method to send data
      "${VISIT_API}",
      headers:
          {"Authorization": "Bearer ${Utils.loginToken}"},
    

    Working fine upto "Facility", but getting error while inserting "Facility: as all others are String values and Facility is of type List

      body: json.encode(
        {
          "fields": {
            "status": {"stringValue": visit.status},
            "id": {"stringValue": visit.id},
            "name": {"stringValue": visit.name},
            "dateTime": {"integerValue": visit.dateTime},
            "mob": {"integerValue": visit.mob},
            "idproof": {"integerValue": visit.idproof},
            "address": {"stringValue": visit.address},
            "purpose ": {"stringValue": visit.purpose},
            "facility": {"arrayValue": visit.facility} //error line
          }
        },
      ),
    );
    print("reach");
    if (response.statusCode == 200) { // successful
      print("visit added");
      return true;
    } else {
      print(response.body);
    }
    } catch (err) {
    throw err;
    }
    }
    

    I do the post-request like

     String VISIT_API =
     "https://firestore.googleapis.com/v1/projects/<database-id>/databases/(default)/documents/visits";
    

    Error message is here

    //###### Error Message #########
    I/flutter (26972): {     //console output
    I/flutter (26972):   "error": {
    I/flutter (26972):     "code": 400,
    I/flutter (26972):     "message": "Invalid JSON payload received. Unknown name \"arrayValue\" at 
    'document.fields[8].value': Proto field is not repeating, cannot start list.",
    I/flutter (26972):     "status": "INVALID_ARGUMENT",
    I/flutter (26972):     "details": [
    I/flutter (26972):       {
    I/flutter (26972):         "@type": "type.googleapis.com/google.rpc.BadRequest",
    I/flutter (26972):         "fieldViolations": [
    I/flutter (26972):           {
    I/flutter (26972):             "field": "document.fields[8].value",
    I/flutter (26972):             "description": "Invalid JSON payload received. Unknown name 
    \"arrayValue\" at 'document.fields[8].value': Proto field is not repeating, cannot start list."  
    I/flutter (26972):           }
    I/flutter (26972):         ]
    I/flutter (26972):       }
    I/flutter (26972):     ]
    I/flutter (26972):   }
    I/flutter (26972): }
    
    //#################################
    
    //visit.facility contains :
    //["lunch" , "dinner"]
    
  • Avinash Mawle
    Avinash Mawle almost 4 years
    Thanks @Juan Laara..It work fine when we add of single array element ony by one manually. But still facing the same problem when I assign list object
  • Juan Lara
    Juan Lara almost 4 years
    Updated answer. You'll need to take the values out of visit.facility and put them into the format above.
  • Avinash Mawle
    Avinash Mawle almost 4 years
    Thanks @Juan Lara. The issue is fixed now. Can you pls share me the syntax for inserting mapValue . I have tried with this link, but unable to do. And can you pls help me to write where and orderBy clause with cloud firestore in API sting
  • Juan Lara
    Juan Lara almost 4 years
    I recommend using the Try this API tool to validate your syntax. I used the createDocument reference to validate an array field. It should help with a map field, too. For your query, use the runQuery reference. If you still get stuck, please start another question with an example.
  • Avinash Mawle
    Avinash Mawle almost 4 years
    Lots of thanks @JuanLaara. All the issues are fixed.
  • LonelyWolf
    LonelyWolf almost 4 years
    @AvinashMawle if the answer helped you and fixed all your problems then you should mark the answer as answered by clicking on the tick icon on the left side of the answer