如何查找 Firebase 文档字段并删除文档
How to find Firebase document field and delete document
我有一个按钮可以让用户删除他们的帐户。当用户注册他们的电子邮件并且名称被保存到名为用户的 Firestore 集合时,我想在集合的文档中搜索 uid 字段等于当前用户 uid 的位置,然后删除该文档。删除帐户有效但文档无效,我没有收到任何错误,我不确定我做错了什么...
CollectionReference users = FirebaseFirestore.instance.collection('users');
TextButton(
onPressed: () async {
try {
await users
.where('uid', isEqualTo: _auth.currentUser!.uid).firestore.doc(users.doc().path).delete();
await _auth.currentUser!.delete().then((value) =>
Navigator.pushReplacement(context, MaterialPageRoute(
builder: (context) => const WelcomeScreen())));
} on FirebaseAuthException catch (error) {
Fluttertoast.showToast( msg: error.message.toString(),
gravity: ToastGravity.TOP,
backgroundColor: Colors.red,
textColor: Colors.white);
} on FirebaseException catch (error) { Fluttertoast.showToast(
msg: error.message.toString(),
gravity: ToastGravity.TOP,
backgroundColor: Colors.red,
textColor: Colors.white);
}
},
child: const Text( 'Delete', style: TextStyle(color: Colors.red),
))
我在开发我的应用程序时遇到了问题,发现了类似的代码,所以这里是解决方案。只需将用户 ID 声明为变量即可,无需像这样(可选)
final uid = _auth.currentUser!.uid; //optional
& 然后在你的代码中使用它:-
await users.doc(uid).delete();
而不是
await users
.where('uid', isEqualTo: _auth.currentUser!.uid).firestore.doc(users.doc().path).delete();
Boom 现在就像一个魅力!
希望这有效!!
我有一个按钮可以让用户删除他们的帐户。当用户注册他们的电子邮件并且名称被保存到名为用户的 Firestore 集合时,我想在集合的文档中搜索 uid 字段等于当前用户 uid 的位置,然后删除该文档。删除帐户有效但文档无效,我没有收到任何错误,我不确定我做错了什么...
CollectionReference users = FirebaseFirestore.instance.collection('users');
TextButton(
onPressed: () async {
try {
await users
.where('uid', isEqualTo: _auth.currentUser!.uid).firestore.doc(users.doc().path).delete();
await _auth.currentUser!.delete().then((value) =>
Navigator.pushReplacement(context, MaterialPageRoute(
builder: (context) => const WelcomeScreen())));
} on FirebaseAuthException catch (error) {
Fluttertoast.showToast( msg: error.message.toString(),
gravity: ToastGravity.TOP,
backgroundColor: Colors.red,
textColor: Colors.white);
} on FirebaseException catch (error) { Fluttertoast.showToast(
msg: error.message.toString(),
gravity: ToastGravity.TOP,
backgroundColor: Colors.red,
textColor: Colors.white);
}
},
child: const Text( 'Delete', style: TextStyle(color: Colors.red),
))
我在开发我的应用程序时遇到了问题,发现了类似的代码,所以这里是解决方案。只需将用户 ID 声明为变量即可,无需像这样(可选)
final uid = _auth.currentUser!.uid; //optional
& 然后在你的代码中使用它:-
await users.doc(uid).delete();
而不是
await users
.where('uid', isEqualTo: _auth.currentUser!.uid).firestore.doc(users.doc().path).delete();
Boom 现在就像一个魅力! 希望这有效!!