首页 > 解决方案 > Firestore 安全规则 Flutter 应用拒绝权限

问题描述

我目前正在尝试将我的 Firebase 后端连接到我的 Flutter 应用程序。get()在向 Firestore创建一个简单的请求后,我每次都会遇到相同的错误:

[cloud_firestore/permission-denied] The caller does not have permission to execute the specified operation.

我尝试过的安全规则:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.time < timestamp.date(2020, 11, 30);
    }
  }
}
allow read, write;
allow read, write: if true;
allow read, write: if request.auth.uid != null;
allow read, write: if request.auth != null;
allow read, write: if auth != null ;

我确实在阳光下尝试了一切,但在读取或写入我的数据库方面没有成功,这让我相信错误出在我的代码中。我已检查以确保 Firebase 已正确初始化,Firebase Core 和 Firebase Firestore 也已正确初始化。

这是 Firebase 问题还是需要执行对 Firestore 的调用的特定方式?

获取请求:

  fetchChatMessages() async {
    var messageSnapShots =
        await FirebaseFirestore.instance.collection('topicofday').get();

    print('Messages: $messageSnapShots');
  }
}

初始化 Firebase:

class MyApp extends StatelessWidget {
  final Future<FirebaseApp> _initialization = Firebase.initializeApp();
  @override
  FutureBuilder build(BuildContext context) {
    return FutureBuilder(
      future: _initialization,
      builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          return buildMaterialApp(context);
        }
        return Center(
          child: Container(
            child: CircularProgressIndicator(
              backgroundColor: Theme.of(context).primaryColor,
            ),
          ),
        );
      },
    );
  }

附加信息:

不确定这是否会影响请求,但我正在使用 Provider 进行状态管理。

标签: fluttergoogle-cloud-firestorefirebase-security

解决方案


@DougStevenson 谢谢你!原来我试图从我的 Firebase 控制台中的错误项目中提取数据。奇怪的场景,但你的评论鼓励我跳出框框思考。

原因是在我的 Runner 文件中放置了来自不同项目的错误 GoogleService-Info.plist 文件。


推荐阅读