【问题标题】:Cannot set user display name in my flutter app无法在我的颤振应用程序中设置用户显示名称
【发布时间】:2021-09-20 15:41:41
【问题描述】:

我一直在尝试在注册时设置我的用户显示名称。我在 stackoverflow 中找到的所有解决方案都建议使用 updateProfile()。但它已被弃用,flutter 文档说要改用 updateDisplayName。

我尝试过这样实现,

 void donorSignUp() async {
    try {
      showLoading();
      await auth
          .createUserWithEmailAndPassword(
              email: email.text.trim(), password: password.text.trim())
          .then((result) {
        String _userId = result.user!.uid;
        _addDonorToFirestore(_userId);
        _clearSignUpControllers();
        result.user?.updateDisplayName(donorModel.value.name);
      });
    } catch (e) {
      debugPrint(e.toString());
      Get.snackbar('Sign Up Failed', 'Try again');
    }
  }

但是,当我尝试在 Drawer 中显示它时,调用 user?.displayName 它什么也没显示。我还检查了donorModel.value.name 包含一个字符串值,我知道它不为空。我该怎么办?

函数donorSignUp() 在提升按钮内调用,

child: ElevatedButton(
                                      child: Text(
                                        'Register',
                                        style: buttonFontSansita,
                                      ),
                                      onPressed: () {
                                        if (authController
                                            .signupFormKey.currentState!
                                            .validate()) {
                                          authController.donorSignUp();
                                        }
                                      },

【问题讨论】:

  • 你能把它称为未来吗?
  • 能否详细说明?
  • donorSignUp() 你从哪里调用它,请分享你调用donorSignUp() 的那些代码
  • 我在提升按钮内调用函数,我已经在问题中更新了它。
  • 您可以在注册完成后使用共享首选项进行存储,并在调用您的抽屉以显示名称时再次调用共享首选项。

标签: firebase flutter dart firebase-authentication


【解决方案1】:

始终拥有实时auth 状态的推荐方法是使用authStateChanges 侦听器:

FirebaseAuth.instance
  .authStateChanges()
  .listen((User? user) {
    if (user == null) {
      print('User is currently signed out!');
    } else {
      print('User is signed in!');
    }
  });

您可以阅读更多关于它的信息here。理想情况下,您可以使用 Provider 使数据在整个应用程序中可用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-07
    • 2021-09-09
    • 2019-12-12
    • 1970-01-01
    • 2021-08-13
    • 2021-03-07
    • 1970-01-01
    • 2019-08-01
    相关资源
    最近更新 更多