繁体   English   中英

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

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

我在我的 flutter 应用程序中添加了一个按钮,允许用户删除他们的帐户。 我还想同时删除 firestore 中存在的用户文档。 我正在尝试使用以下代码来实现此目的但遇到错误。

在下面的代码中,我得到了与用户文档名称相同的用户 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);

现在我要删除用户集合中当前用户的文档:

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);
                    });
              },

但这会在按下时引发错误:

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

在我有 Streambuilder 的任何地方都会重复此错误。 为什么会发生此错误,我还需要如何从 firestore 中删除当前用户文档?

当使用它们的页面弹出时,您是否处理这些流? 在我看来,删除您的用户效果很好,但是您的 stream 然后尝试从 Firestore 中获取已删除用户的数据。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM