本文介绍了在Flutter中使用REST API将列表数据发送到Cloud Firestore时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法在Flutter中使用REST API将字符串列表发送到Cloud Firestore.
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}"},
可以正常工作到"Facility",但在插入"Facility"时会出错:因为所有其他都是字符串值,而Facility的类型为List
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;
}
}
我做类似的后请求
String VISIT_API =
"https://firestore.googleapis.com/v1/projects/<database-id>/databases/(default)/documents/visits";
错误消息在这里
//###### 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"]
推荐答案
尝试像这样编写您的 facility
数组值:
Try writing your facility
array value like this:
"facility": {
"arrayValue": {
"values": [
{
"stringValue": "lunch"
},
{
"stringValue": "dinner"
}
]
}
}
值"
部分来自对值和 arrayValue
这篇关于在Flutter中使用REST API将列表数据发送到Cloud Firestore时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!