Firebase 权限被拒绝:缺少权限或权限不足

Firebase 权限被拒绝:缺少权限或权限不足,firebase,flutter,google-cloud-firestore,firebase-security,Firebase,Flutter,Google Cloud Firestore,Firebase Security,我正在运行以下firebase代码 FirebaseApp app = await FirebaseApp.configure( name: 'weather app', options: const FirebaseOptions( googleAppID: '1:nnnnnnnnn:android:nnnnnnn', apiKey: 'XXXXXXXX', projectI

我正在运行以下firebase代码

    FirebaseApp app = await FirebaseApp.configure(
          name: 'weather app',
          options: const FirebaseOptions(
            googleAppID: '1:nnnnnnnnn:android:nnnnnnn',
            apiKey: 'XXXXXXXX',
            projectID: 'XXXXX-XXX-XXX08',
          ),
        );
        Firestore firestore = Firestore(app: app);
        FirebaseAuth.fromApp(app).signInAnonymously();
如果我第一次在emulator中运行此代码,它将抛出以下错误

 I/flutter (17766): getConfigState ERROR 2 PlatformException(Error performing get, PERMISSION_DENIED: Missing or insufficient permissions., null)
W/Firestore(17766): (21.3.0) [Firestore]: Listen for Query(weather-api-info/owm) failed: Status{code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null}
但如果我只是使用热加载或完全加载来重新加载应用程序,那么它不会抱怨。同样,只有当应用程序第一次在设备上运行时才会出现问题(我可以通过卸载应用程序并重试来重新生成此问题)

云firestore验证如下图所示(我不打算设置任何登录或验证,但也不想让它保持打开状态

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

总之,我没有为异步调用正确设置代码。 以下是我修改的代码(将下面提到的代码与原始帖子中的代码进行比较)

Future getOWMFirestoreData()异步{
试一试{
FirebaseApp app=等待FirebaseApp.configure(
名称:'天气应用',
选项:常量FirebaseOptions(
googleAppID:'1:nnnnn:android:nnnnnnn',
apiKey:'XXXXXXXX',
项目名称:“XXXXX-XXX-XXX08”,
),
);
等待FirebaseAuth.fromApp(app).signinanoymously();
Firestore Firestore=Firestore(应用程序:应用程序);
var文件=
等待firestore.collection('XXX-info').document('owm').get();
debugPrint('getOWMFirestoreData successfull${docs.exists}');

您的安全规则要求用户在查询时登录。那@DougStevenson,有没有办法让访问保持关闭而不登录,或者如何以管理员身份登录..?还有,为什么同样的代码会在第二次运行时毫无怨言?没有办法将Firestore查询仅限于您的应用程序。Y您必须使用Firebase Auth来限制访问。@KenWhite刚刚做了,点击了保存按钮,太快了…@DougStevenson谢谢,但是,如果我只是使用hotreload第二次重新加载代码,同样的代码也可以工作..**output**
I/flatter(17766):getConfigState from Firebasehttps://community-open-weather-map.p.rapidapi.com/weather/
Future<void> getOWMFirestoreData() async {
  try {
    FirebaseApp app = await FirebaseApp.configure(
      name: 'weather app',
      options: const FirebaseOptions(
            googleAppID: '1:nnnnnnnnn:android:nnnnnnn',
            apiKey: 'XXXXXXXX',
            projectID: 'XXXXX-XXX-XXX08',
      ),
    );
    await FirebaseAuth.fromApp(app).signInAnonymously();
    Firestore firestore = Firestore(app: app);

    var docs =
        await firestore.collection('XXX-info').document('owm').get();
    debugPrint('getOWMFirestoreData Succesfull ${docs.exists}');