我正在尝试将文档上传到云中。本文档应Cloud功能以后的要求提供。无论如何,我编写文档的代码:
try {
Firestore.instance.collection('cloud_requests').document().setData(
{'UID': user.uid, 'UDID': cameraScanResult, 'request': 'add'});
} on PlatformException catch (e) {
print(e);
Alert(
context: context,
type: AlertType.error,
title: "Something went wrong.",
buttons: [
DialogButton(
onPressed: () {
Navigator.pop(context);
},
color: Colors.red,
child: Text(
"Ok",
style: TextStyle(color: Colors.white, fontSize: 20),
),
),
]).show();
return;
}
问题是,由于我设置了这样的规则,因此故意丢失了权限错误:
match /cloud_requests/{cloudRequest} {
function isUser() {
return request.auth != null && (request.resource.data.UID == request.auth.uid);
}
function isValidDevice() {
return exists(/databases/$(database)/documents/devices/$(request.resource.data.UDID));
}
function isValidRequest() {
return (request.resource.data.request == "add" || request.resource.data.request == "remove")
}
allow write: if (isUser() && isValidDevice() && isValidRequest());
allow read: if false;
}
尽管尝试通过try and catch或使用.catchError()
选项来捕获错误,但似乎无法捕获它。
答案 0 :(得分:0)
对于任何其他想知道的人:我还没有解决捕获错误的问题,它仍然在调试模式下出现,但是我至少做到了,以便警报能够按需显示。
await Firestore.instance
.collection('cloud_requests')
.document()
.setData({'UID': user.uid, 'UDID': cameraScanResult, 'request': 'add'})
.then((value) => Alert(
context: context,
type: AlertType.success,
title: "Success.",
desc: 'Device ' + cameraScanResult + ' will be visible soon.',
buttons: [
DialogButton(
onPressed: () {
Navigator.pop(context);
},
color: Colors.green,
child: Text(
"Ok",
style: TextStyle(color: Colors.white, fontSize: 20),
),
),
]).show())
.catchError((e) => Alert(
context: context,
type: AlertType.error,
title: "Something went wrong.",
desc: 'Please try to scan again.',
buttons: [
DialogButton(
onPressed: () {
Navigator.pop(context);
},
color: Colors.red,
child: Text(
"Ok",
style: TextStyle(color: Colors.white, fontSize: 20),
),
),
]).show());
我先使用.then()
,然后再使用.catchError()
。似乎目前可以使用。