【问题标题】:I/flutter (14318): NoSuchMethodError: Class 'FirebaseUser' has no instance getter 'firstname'I/flutter (14318): NoSuchMethodError: Class 'FirebaseUser' has no instance getter 'firstname'
【发布时间】:2019-01-17 07:05:34
【问题描述】:

我正在尝试更新我正在构建的应用程序用户的用户个人资料。用户信息存储在 firebase 中,应用程序在 android 上运行的 dart 中编码。

我已经尝试从https://firebase.google.com/docs/auth/web/manage-users 网站实现示例。

 51   createUser() {
 52     if (checkFields()) {
 53       //Perform Login
 54       var user = FirebaseAuth.instance.currentUser();
 55       FirebaseAuth.instance
 56           .createUserWithEmailAndPassword(email: _email, password: _password)
 57           .then((user) {
 58             var  userUpdateInfo = new UserUpdateInfo();
 59             user.updateProfile(userUpdateInfo)
                    .then((user) {
 60                   userUpdateInfo.displayName = _Firstname;
 61                   FirebaseAuth.instance
                                  .currentUser()
                                  .then((user)  {
                                    UserManagement().storeNewuser(user, context);
 62                               })
                                  .catchError((e) {print(e);});
 63                 })
                    .catchError((e) {
 64                   print(e);
 65                 });
 66                 //UserManagement().storeNewuser(user, context);
 67                 Navigator.of(context).pop();
 68                 Navigator.of(context)
                             .pushReplacementNamed('/landingpage');
 69           })
              .catchError((e) {
 70             print("ivlvvliyviv");
 71           });
 72     }
 73   }

应用程序运行平稳,不会崩溃,但是当用户更新名字但在 firebase 上没有更改时,第 60 行假设更新 firebase 上的名字字段,这就是我添加名字字段的地方

Firestore.instance
         .collection('/users')
         .add({ 
           'email': user.email, 
           'uid': user.uid, 
           'firstname': user.firstname,

【问题讨论】:

  • 一旦创建 - 你只是想更新用户 - displayName ?
  • 'firstname': user.firstname 这永远不会起作用 - Firebase 用户没有这样的字段。
  • 是的,我想更新用户显示名,我该怎么做
  • 我添加了关于更新的答案 - User - displayName

标签: firebase dart flutter


【解决方案1】:

为了更新 Firebase User - displayName。您需要对代码进行一些更改。

在您的 .then 函数之外制作 - var userUpdateInfo = new UserUpdateInfo(); 状态变量。然后拨打user.updateProfile

如下编辑您的创建用户代码:

FirebaseAuth.instance.createUserWithEmailAndPassword(email: _email, password: _password)
      .then((user) {
    var  userUpdateInfo = new UserUpdateInfo();
    userUpdateInfo.displayName = _Firstname;  // Pass the value you want as displayName
    user.updateProfile(userUpdateInfo).then((val){  // will Update the User at Firebase Auth
      print('User Display Name Updated.');
    });
  });

【讨论】:

  • 对状态变量的工作原理以及我的 .then 函数之外的状态变量有点困惑,你是什么意思?
  • 没关系-您可以忽略该点并添加-var userUpdateInfo = new UserUpdateInfo();before-userUpdateInfo.displayName = _Firstname;我会更新答案。如果您对此有任何问题,请告诉我。
  • 是的,我有一个问题,它仍然没有在我的 firebase 数据库中添加名字的字段
  • 那是单独的问题 - 更新您的问题 - 请注意 Firebase Auth 用户和您在数据库中手动保存的用户是不同的。
  • Firebase Auth 用户没有任何名字字段,它只有 displayName
猜你喜欢
  • 2021-03-02
  • 2020-01-18
  • 1970-01-01
  • 2022-11-15
  • 2019-06-13
  • 1970-01-01
  • 1970-01-01
  • 2020-08-11
  • 2020-05-05
相关资源
最近更新 更多