Firebase Firestore响应要求为操作提供索引

z9smfwbn  于 2023-03-24  发布在  其他
关注(0)|答案(1)|浏览(151)

我已经编写了Flutter代码,涉及Firebase和Firestore,用于在当前用户登录后立即检索有关他们的数据。
我的期望是Firestore在Firestore中确认数据,但Firestore却请求了一个索引。
我的问题是不知道如何生成所请求的索引。
代码如下所示:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart' as auth;

import '../widgets/textfield_try.dart';

class LoginScreen extends StatelessWidget {
  LoginScreen({super.key, this.onTap});

  TextEditingController emailController = TextEditingController();
  TextEditingController passwordController = TextEditingController();
  auth.FirebaseAuth _auth = auth.FirebaseAuth.instance;
  FirebaseFirestore _dB = FirebaseFirestore.instance;
  Function()? onTap;
  static const source = Source.serverAndCache;

  Future<void> signUserIn() async {
    await auth.FirebaseAuth.instance.signInWithEmailAndPassword(
        email: emailController.text, password: passwordController.text);
    if (_auth != null) {
      try {
        final docRef = await _dB.collectionGroup('signInData').where(
            'eAddress', isEqualTo: _auth.currentUser!.email).get().then((
            value) {
          final userData = value.docs.toList();
          print(userData);
        });
      } on Exception catch (e) {
        print(e);
      }
    }
  }

@override
Widget build(BuildContext context) {
  return MaterialApp(
    home: Scaffold(
      resizeToAvoidBottomInset: false,
      backgroundColor: Colors.blue[900],
      body: SafeArea(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Padding(padding: EdgeInsets.only(top: 0, bottom: 37)),

              TextField_Try(
                  hintText: 'Username',
                  obscureText: false,
                  controller: emailController),

              Padding(padding: EdgeInsets.only(top: 0, bottom: 0)),

              Padding(
                  padding: EdgeInsets.only(
                      top: 8.0, left: 8.0, right: 8.0, bottom: 28.0),
                  child: TextField_Try(
                    hintText: '**************',
                    obscureText: true,
                    controller: passwordController,
                  )),

              Padding(padding: EdgeInsets.only(bottom: 10)),

              ElevatedButton(onPressed: signUserIn, child: Text('Sign In')),

              Padding(padding: EdgeInsets.only(bottom: 25)),
            ],
          ),
        ),
      ),
    ),
  );
}
}

Flutter对代码的响应是:

D/FirebaseAuth(12349): Notifying id token listeners about user ( ************************ ).
I/flutter (12349): [cloud_firestore/failed-precondition] Operation was rejected because the system is not in a state required for the operation's execution. If performing a query, ensure it has been indexed via the Firebase console.

我的问题是,如何为基于单个collection-id的collection-group查询生成索引?
谢谢你考虑我的问题。

suzh9iv8

suzh9iv81#

由于您要查询collectionGroup('signInData'),因此需要在该集合组上创建一个索引。在本例中,您需要在signInData集合组的eAddress字段上创建一个单字段索引。
要在Firebase控制台中执行此操作,请执行以下操作:
1.转到Firebase控制台中的Firestore indexes页面
1.单击Single field面板,因为您要在单个字段上添加索引。
1.单击面板底部附近的“Add excemption”按钮

1.在“集合ID”字段中输入signInData,并将eAddress作为“字段”路径。
1.勾选收藏组框

最后:单击下一步,然后单击保存

相关问题