我已经编写了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查询生成索引?
谢谢你考虑我的问题。
1条答案
按热度按时间suzh9iv81#
由于您要查询
collectionGroup('signInData')
,因此需要在该集合组上创建一个索引。在本例中,您需要在signInData
集合组的eAddress
字段上创建一个单字段索引。要在Firebase控制台中执行此操作,请执行以下操作:
1.转到Firebase控制台中的Firestore indexes页面
1.单击
Single field
面板,因为您要在单个字段上添加索引。1.单击面板底部附近的“Add excemption”按钮
1.在“集合ID”字段中输入
signInData
,并将eAddress
作为“字段”路径。1.勾选收藏组框
最后:单击下一步,然后单击保存