【问题标题】:How to find Firebase document field and delete document如何查找 Firebase 文档字段并删除文档
【发布时间】:2022-01-01 23:55:38
【问题描述】:

我有一个按钮可以让用户删除他们的帐户。当用户注册他们的电子邮件并将姓名保存到名为 users 的 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),
  ))

【问题讨论】:

  • 请告知您遇到的错误可能有助于更多地了解您的问题
  • 由于某种原因,我在调试终端中没有收到错误
  • 试试我的答案,如果它不起作用,请告诉我

标签: firebase flutter dart google-cloud-firestore firebase-authentication


【解决方案1】:

我刚刚在我的应用程序上工作时遇到了问题,并找到了类似的代码,所以这里是解决方案。只需将用户 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 现在它就像一个魅力! 希望这行得通!

【讨论】:

  • 非常感谢,它有效
猜你喜欢
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 2019-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多