【问题标题】:Firebase : Why appearing [clound_firestore/permission-denied] for my firestoreFirebase:为什么我的 Firestore 出现 [cloud_firestore/permission-denied]
【发布时间】:2021-12-29 10:00:17
【问题描述】:

我知道有很多类似的帖子,但我仍然无法为我的问题找到自己的答案。由于我想在不按下按钮的情况下从 firebase 获取数据并且可以直接从中获取数据,因此我使用 StreamBuilder 进行编码,但仍然出现此错误。这是我的 Firestore 数据库还是实时数据库权限有问题?

--问题警告--

下面是我的代码:

import 'dart:async';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:profile_staff/edit_profile_page.dart';
import 'package:profile_staff/profile_widget.dart';
import 'package:profile_staff/user.dart';
import 'package:profile_staff/user_preferences.dart';

class ProfilePage extends StatefulWidget {
  const ProfilePage({Key? key}) : super(key: key);

  @override
  _ProfilePageState createState() => _ProfilePageState();
}

class _ProfilePageState extends State<ProfilePage> {
  final Stream<QuerySnapshot> users =
      FirebaseFirestore.instance.collection('users').snapshots();
  TextEditingController _controller = TextEditingController();

  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final user = UserPreferences.myUser;

    return Scaffold(
      appBar: AppBar(
        leading: new IconButton(
            onPressed: () {}, icon: new Icon(Icons.arrow_back_ios_sharp)),
        title: Center(
          child: Text(
            'My Profile',
            style: TextStyle(color: Colors.white),
          ),
        ),
        actions: [
          IconButton(
              onPressed: () {
                Navigator.of(context).push(
                  MaterialPageRoute(builder: (context) => EditProfilePage()),
                );
              },
              icon: new Icon(Icons.create_outlined))
        ],
        backgroundColor: Colors.green,
        shadowColor: Colors.white,
        elevation: 3,
      ),
      body: Padding(
        padding: const EdgeInsets.all(20.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              'Read Data',
              style: TextStyle(fontSize: 20, fontWeight: FontWeight.w600),
            ),
            Container(
                height: 250,
                padding: const EdgeInsets.symmetric(vertical: 20),
                child: StreamBuilder<QuerySnapshot>(
                  stream: users,
                  builder: (
                    BuildContext context,
                    AsyncSnapshot<QuerySnapshot> snapshot,
                  ) {
                    // if (!snapshot.hasData) {
                    //   return Text('error404');
                    // }
                    
                    if (snapshot.connectionState == ConnectionState.waiting) {
                      return Text('Loading');
                    }

                    final data = snapshot.requireData;

                    return ListView.builder(
                      itemCount: data.size,
                      itemBuilder: (context, index) {
                        return Text(
                            'My name is ${data.docs[index]['name']} and I am ${data.docs[index]['age']}');
                      },
                    );
                  },
                ))
          ],
        ),
       
}

我的 Firestore 数据库:

我的实时数据库:

enter image description here

enter image description here

【问题讨论】:

  • 你确定你指向的是同一个数据库吗?
  • 我不确定,因为这是我第一次在flutter中使用firebase。我真的在编码中使用了错误的数据库吗?
  • 您的安全规则要求用户登录 Firebase 身份验证才能访问数据库。由于规则拒绝读取操作,看来用户没有登录。我建议看看firebase.flutter.dev/docs/auth/overview

标签: firebase flutter google-cloud-firestore firebase-security


【解决方案1】:

我遇到了同样的问题。忘记部署/上传新规则

【讨论】:

  • 同样的问题?你的意思是我的数据库规则出错了,但我也不知道如何编辑?我也尝试了基于互联网材料的规则,但仍然没有工作。
猜你喜欢
  • 2021-09-17
  • 2019-05-03
  • 2013-06-07
  • 2023-02-06
  • 2013-10-20
  • 2016-06-07
  • 1970-01-01
  • 2015-03-09
  • 1970-01-01
相关资源
最近更新 更多