[英]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.