简体   繁体   English

如何删除 firestore 集合中的当前用户文档?

[英]How to delete current user document in firestore collection?

I am adding a button in my flutter app which allows users to delete their account.我在我的 flutter 应用程序中添加了一个按钮,允许用户删除他们的帐户。 I also want to delete the user document present in firestore simultaneously.我还想同时删除 firestore 中存在的用户文档。 I am trying to achieve this using below code but facing an error.我正在尝试使用以下代码来实现此目的但遇到错误。

In below code, I am getting the user uid which is same as the user's doc name:在下面的代码中,我得到了与用户文档名称相同的用户 uid:

CollectionReference users = FirebaseFirestore.instance.collection('users');

final FirebaseAuth auth = FirebaseAuth.instance;
final User? user = auth.currentUser;
final uid = user?.uid;
final authService = Provider.of<AuthService>(context);

Now I am deleting the current user's document in the users collection:现在我要删除用户集合中当前用户的文档:

ElevatedButton(
              onPressed: ()async{
               await users.doc(uid).delete();  //delete doc
                user!.delete().then((value) async { //delete user
                await authService.signOut();
                Navigator.pushAndRemoveUntil(
                    context,
                    MaterialPageRoute(
                    builder: (context) => LoginScreen(),
                    ),
                   (Route<dynamic> route) => false);
                    });
              },

But this throws an error when pressed:但这会在按下时引发错误:

type 'Null' is not a subtype of type 'Map<String, dynamic>' in type cast

The relevant error-causing widget was
StreamBuilder<DocumentSnapshot<Object?>>
lib\screens\quiz_screen.dart:616
When the exception was thrown, this was the stack
#0      CoinsContainer.build.<anonymous closure>
lib\screens\quiz_screen.dart:630

This error repeats everywhere I have a Streambuilder.在我有 Streambuilder 的任何地方都会重复此错误。 Why does this error occur and how else do I need to delete the current user document from firestore?为什么会发生此错误,我还需要如何从 firestore 中删除当前用户文档?

Do you dispose the streams when the page in which they are used is popped?当使用它们的页面弹出时,您是否处理这些流? It seems to me that the deletion of your user works just fine, but that your stream then tries to fetch a deleted user's data from Firestore.在我看来,删除您的用户效果很好,但是您的 stream 然后尝试从 Firestore 中获取已删除用户的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不删除firestore中的集合的情况下删除文档? - how to delete document without deleting collection in firestore? 如何在 Google Firestore 中获取当前登录用户的文档 - How to get document by current signed in user in Google Firestore 再次删除带有文档和集合的集合(firestore react native) - delete a collection with document and collection again inside (firestore react native) 如何删除 Firestore 文档中的字段? - How to delete a field in a Firestore Document? 用户在 Cloud firestore 中注册时如何将当前用户 uid 添加到用户集合? - how to add current user uid to user collection when user signup in cloud firestore? 如何使用 Flutter 删除 Firestore 中集合中的所有文档 - How to Delete all documents in collection in Firestore with Flutter 如何使用 Flutter 在 FireStore 的集合中创建文档 - How to create document in collection in FireStore with Flutter 将集合添加到 Firestore 中的文档 - Adding a collection to a document in Firestore 如何使用 Flutter 从 Firestore 获取当前用户文档 ID/名称? - How can I get current user document id/name from Firestore with Flutter? 如何使用 firestore wherefield 方法并将该集合的文档添加到另一个集合 - how to use firestore wherefield method and adding document that collection to another collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM