简体   繁体   English

未经授权拒绝 Firestore 权限

[英]Firestore permission denied without auth

So I'm new to Firestore and I created a database, I added an android app, followed the steps... But when I want to query the base I always get a "Permission denied" warning" even tho I changed the security rules of the database to allow every user to read and write on it. I just wanna use as a dummy for now without having to authenticate, is that possible所以我是 Firestore 的新手,我创建了一个数据库,我添加了一个 android 应用程序,按照步骤操作......但是当我想查询数据库时,我总是收到“权限被拒绝”警告“即使我更改了安全规则允许每个用户读取和写入数据库。我现在只想用作虚拟对象而无需进行身份验证,这可能吗

main.dart:主要.dart:

import 'package:chat_app/screens/chat_screen.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: ChatScreen(),
    );
  }
}

other_screen:其他屏幕:

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';

class ChatScreen extends StatelessWidget {
  const ChatScreen({super.key});


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: StreamBuilder(
        stream: FirebaseFirestore.instance
            .collection('chats')
            .snapshots(),
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return CircularProgressIndicator();
          }
          final documents = snapshot.data!.docs;
          return ListView.builder(
            itemCount: documents.length,
            itemBuilder: (context, index) => Container(
              padding: EdgeInsets.all(8),
              child: Text(documents[index]['text']),
            ),
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        child: Icon(Icons.add),
      ),
    );
  }
}

Even after changing the security rules I still get the error即使在更改安全规则后我仍然收到错误

The "Permission denied" warning you're getting is likely caused by the security rules you've set in your Firestore database.您收到的“权限被拒绝”警告可能是由您在 Firestore 数据库中设置的安全规则引起的。 Based on the code you've provided, you're trying to read from a collection called "chats" in your Firestore database, but the security rules you've set don't allow that.根据您提供的代码,您正尝试从 Firestore 数据库中名为“聊天”的集合中读取数据,但您设置的安全规则不允许这样做。

You can try the following steps:您可以尝试以下步骤:

  1. Go to the Firebase console, click on the Firestore tab, and then click on the "Rules" tab. Go 到 Firebase 控制台,点击 Firestore 选项卡,然后点击“规则”选项卡。

2.Make sure the rules for your database are set to allow read and write access for all users, like this: 2.确保您的数据库规则设置为允许所有用户进行读写访问,如下所示:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

3.Save the rules and wait for them to propagate. 3.保存规则并等待它们传播。 This can take a few minutes.这可能需要几分钟时间。 Also make sure that you have added the correct google-services.json file to your android project and also the correct google-services.plist file to your ios project.还要确保您已将正确的 google-services.json 文件添加到您的 android 项目,并将正确的 google-services.plist 文件添加到您的 ios 项目。 And make sure that you are calling the Firebase.initializeApp() before trying to access the database.并确保在尝试访问数据库之前调用 Firebase.initializeApp()。

If you still get the error, please make sure that the Firestore is enabled in the Firebase console for your project.如果仍然出现错误,请确保在 Firebase 控制台中为您的项目启用了 Firestore。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM