firebase - 在颤动中使用 REST api 将列表数据发送到云 Firestore 时出错
问题描述
我无法在颤动中使用 REST api 将字符串列表发送到云防火墙。
//###### 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}"},
在“设施”之前工作正常,但在插入“设施”时出错:因为所有其他都是字符串值,设施是列表类型
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
数组值:
"facility": {
"arrayValue": {
"values": [
{
"stringValue": "lunch"
},
{
"stringValue": "dinner"
}
]
}
}
该"values"
部分来自对Value和arrayValue的引用。
推荐阅读
- python - BS4 通过抓取表格给出一个奇怪的随机输出
- java - Java Selenium WebDriverWait 等待的时间比指定的等待时间长
- python - 为什么 pdfplumber 不产生任何数据?
- javascript - 如何在模板中通过javascript对从django传递的json进行编码
- java - 从响应中保存值并检查是否为空加特林
- google-apps-script - Google 幻灯片:通过脚本操作指南
- fortran - 可视化 Fortran 程序的结构
- c# - 使用 TLSharp 创建一个新的 Telegram 频道并修复权限/权限
- java - 我想制作一个带有处理的二维数组
- android - 如何使用共享首选项在活动之间切换?