【问题标题】:Handling Firebase Insufficient Permissions Errors in FlutterFlutter 中处理 Firebase 权限不足错误
【发布时间】:2019-07-31 21:20:26
【问题描述】:

如何处理来自 firebase 的权限不足错误?我目前这样做:

      try {
        DocumentSnapshot doc = await Firestore.instance.collection('SomeCollection').document('SomeDocument').get();
        // Do something
      } catch (error) {
        print('Error: $error');
        // Do something to show user
      }

我不能只检查权限错误。我怎样才能只捕获权限不足的错误?

我什么时候应该使用.catchError(),我试过这个:

        DocumentSnapshot doc = await Firestore.instance.collection('Some Collection').document('Some Document').get().catchError((onError) {
          // What to do here
        });

我似乎并没有真正捕捉到错误,因为仍然抛出异常

【问题讨论】:

  • 运气好!?当用户没有足够的权限时,我的 firebase 会退回到加载离线数据。尽管错误已记录在控制台中,但我终其一生都无法控制地捕捉错误,并对其做出正确反应!

标签: firebase dart flutter google-cloud-firestore


【解决方案1】:

这应该可以工作

 DocumentSnapshot doc = await Firestore.instance.collection('Some Collection').document('Some Document').get().catch((err)=>print(err));

【讨论】:

  • 我不想允许阅读该文档!我的安全规则应该防止文档被阅读!我只想捕捉权限不足的异常,只有这个异常!
  • 错误看起来像:6.0.0 - [Firebase/Firestore][I-FST000001] Listen for query at <collection name> failed: Missing or insufficient permissions. 我们想要捕获错误并对其做出反应。 Firebase 在控制台中记录错误,但不会在代码中抛出错误!相反,它会从用户上次登录时开始加载离线数据。
  • @Jonas 我刚刚修改了答案,在查询末尾添加了捕获
【解决方案2】:

您可以通过错误代码(即'permission-denied')识别权限错误。 如果是不同的错误,你可以抛出它,或者以不同的方式处理。

try {
    DocumentSnapshot doc = await Firestore.instance.collection('SomeCollection').document('SomeDocument').get();
    // Do something
  } catch (error) {
    if (error.code === 'permission-denied') {
      // Do something to show user
    }
    else {
      throw new Error(e); // Or do other things
    }
  }

【讨论】:

  • 嗨,莫舍。虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。 How to Answer。亲切的问候。
猜你喜欢
  • 2021-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多