在Flutter上删除用户数据后,如何删除Firebase帐户? [英] How to delete firebase account when user data is deleted on flutter?

查看:45
本文介绍了在Flutter上删除用户数据后,如何删除Firebase帐户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在flutter中的身份验证中删除Firebase帐户?如果是,该怎么做?我一直在搜索,但找不到路.

is it possible to delete firebase account in authentication on flutter? if yes, how to do that? I have been search but not found the way.

Firestore.instance.collection("users").document(uid).delete().then((_){
   // delete account on authentication after user data on database is deleted            
});

推荐答案

如果使用flutter,如果要删除Firebase帐户以及关联的Firestore用户收集文档,则可以使用以下方法. (由Firebase uid命名的用户集合中的文档).

Using flutter, if you want to delete firebase accounts together with the associated firestore user collection document, the following method works fine. (documents in user collection named by the firebase uid).

数据库类

class DatabaseService {
  final String uid;

  DatabaseService({this.uid});

  final CollectionReference userCollection =
      Firestore.instance.collection('users');

  Future deleteuser() {
    return userCollection.document(uid).delete();
  }
}

否则,请使用Firebase版本0.15.0或更高版本,Firebase的reauthenticateWithCredential()方法会引发类似{noSuchMethod:在null上被调用}的错误.

Use Firebase version 0.15.0 or above otherwise, Firebase reauthenticateWithCredential() method throw an error like { noSuchMethod: was called on null }.

身份验证类

class AuthService {
  final FirebaseAuth _auth = FirebaseAuth.instance;

Future deleteUser(String email, String password) async {
    try {
      FirebaseUser user = await _auth.currentUser();
      AuthCredential credentials =
          EmailAuthProvider.getCredential(email: email, password: password);
      print(user);
      AuthResult result = await user.reauthenticateWithCredential(credentials);
      await DatabaseService(uid: result.user.uid).deleteuser(); // called from database class
      await result.user.delete();
      return true;
    } catch (e) {
      print(e.toString());
      return null;
    }
  }
}

然后在flift小部件树的clickable事件中使用以下代码来实现目标;

Then use the following code inside the clickable event of a flutter widget tree to achieve the goal;

onTap: () async {  
     await AuthService().deleteUser(email, password);
}

这篇关于在Flutter上删除用户数据后,如何删除Firebase帐户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆