首页 > 解决方案 > 当用户没有足够的权限写入数据库时​​,如何捕获 firestore 权限被拒绝异常?

问题描述

当我尝试使用没有写入权限的用户帐户写入数据库时​​,我按预期得到了这个异常

发生异常。PlatformException (PlatformException(firebase_firestore, com.google.firebase.firestore.FirebaseFirestoreException: PERMISSION_DENIED: Missing or enough permissions., {code: permission-denied, message: 调用者没有执行指定操作的权限。}))

但我无法抓住它。

调用此方法会导致异常

Future<void> addRecord(Product product) async {
    try {
      await FirebaseFirestore.instance
          .collection("products")
          .doc()
          .set(product.toMap())
          .catchError((err) {
        print("error is" + err.toString());
      });
    } on PlatformException catch (e) {
      print("eee" + e.toString());
    }
  }

我相信这个异常是在控制台中捕获的,我收到了这条消息:

eee[cloud_firestore/permission-denied] 调用者没有执行指定操作的权限。

但是控制台中还有一条消息

W/Firestore(12930): (21.4.3) [Firestore]: 在 products/SPeUO9260uCoIfpDsSag 写入失败:状态{code=PERMISSION_DENIED,描述=缺少或权限不足。,原因=null}

标签: firebaseflutterdartgoogle-cloud-firestoreerror-handling

解决方案


推荐阅读