简体   繁体   English

Firebase 用户的显示名称在注册后没有更新。 我需要重新启动应用程序才能使其正常工作

[英]Firebase user's displayName does not update after register. I need to restart the app to make it work

I want the firebase user's displayName to update as soon as it registers so that I can display the first letter of their name in a drawer.我希望 firebase 用户的显示名称在注册后立即更新,以便我可以在抽屉中显示他们姓名的第一个字母。 After I register the user with displayName and open the profile drawer it shows an error saying that displayName[0] is null but as soon as I restart the app can recognize the user.在我使用 displayName 注册用户并打开配置文件抽屉后,它显示一条错误消息,指出 displayName[0] 是 null 但只要我重新启动应用程序就可以识别用户。 How do I make the app recognize the user's name after registration?如何让应用程序在注册后识别用户名?

Authentication code验证码

class AuthService {
  final FirebaseAuth _auth = FirebaseAuth.instance;

  UserModel? _userFromFirebaseUser(User? user) {
    return user != null
        ? UserModel(
            uid: user.uid,
            email: user.email ?? '',
            displayName: user.displayName ?? '',
          )
        : null;
  }


  Future signInWithEmailAndPassword(String email, String password) async {
    try {
      UserCredential result = await _auth.signInWithEmailAndPassword(
        email: email,
        password: password,
      );
      User user = result.user!;
      return user;
    } catch (error) {
      return null;
    }
  }
}

Register Page注册页面


  final AuthService _auth = AuthService();
  final _formKey = GlobalKey<FormState>();
  bool isLoading = false;

  String email = '';
  String password = '';
  String name = '';
  String error = '';

  void register(String email, String password, String name) async {
    if (_formKey.currentState!.validate()) {
      setState(() => isLoading = true);
      dynamic result = await _auth.registerWithEmailAndPassword(email, password, name);

      if (result == null) {
        setState(() => isLoading = false);
        setState(() => error = 'Please supply a valid email');
      }
    }
  }

Widget where the name is diplayed显示名称的小部件


    Widget build(BuildContext context) {
    final user = Provider.of<UserModel?>(context);

    String? displayName = user!.displayName;
    String profileDisplayName = user.displayName[0];

    .
    .
    .

    CircleAvatar(
        radius: 50,
        backgroundColor: Colors.orangeAccent,
        child: Center(
          child: Text(
             profileDisplayName,
             style: const TextStyle(
                fontSize: 50,
                color: Colors.white,
               ),
             ),
           ),
        ),
     const SizedBox(height: 10),
     Text(
       displayName,
       style: const TextStyle(
       fontSize: 20,
       color: Colors.white,
       letterSpacing: 0.6,
       ),
     ),

I tried to reload the app using initState() but that didn't work, I tried to user reload() function in the auth service but that didn't work either.我尝试使用 initState() 重新加载应用程序,但这没有用,我尝试在身份验证服务中使用 reload() function,但这也没有用。

reload your user data and fetch again重新加载您的用户数据并再次获取

class AuthService {
          final FirebaseAuth _auth = FirebaseAuth.instance;
        
          UserModel? _userFromFirebaseUser(User? user) {
            return user != null
                ? UserModel(
                    uid: user.uid,
                    email: user.email ?? '',
                    displayName: user.displayName ?? '',
                  )
                : null;
          }
        
        
          Future signInWithEmailAndPassword(String email, String password) async {
            try {
              UserCredential result = await _auth.signInWithEmailAndPassword(
                email: email,
                password: password,
              );
              User user = result.user!;
              await user.reload();
              user = await _auth.currentUser();
              return user;
            } catch (error) {
              return null;
            }
          }
        }

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

相关问题 Email 和在 React 应用程序中使用 firebase 的密码注册,用户的显示名称在初始渲染中显示为 null。 我怎样才能解决这个问题? - Email and Password Signup using firebase in a react app , the displayName of the user appears to be null on the initial render. How can I fix this? 注册时更新显示名称 Firebase Flutter - Update displayName while signup Firebase Flutter Firebase:如何在应用检查中注册我的 Flutter 应用? - Firebase: How do I register my Flutter app in app check? Flutter:注册成功后如何让用户自动登录? - Flutter: How to make user login automatically after register successfully? 如何将用户的显示名称添加到 Firestore 数据库集合条目中? - How can a user's displayName be added to a Firestore database collection entry? 即使在具有 firebase 身份验证的 nextjs 应用程序中重新加载页面后,如何保持用户登录? - how can I keep a user looged in even after page reload in nextjs app with firebase authentication? Firebase Login Register App with DeviceID - Firebase Login Register App with DeviceID 更新后应用程序未构建 Firebase 性能 SDK - App isn't building after update Firebase Performance SDK Firebase 登录/注册后无限加载 - Firebase Infinite Loading after Login/Register 我是否需要 Firebase 项目才能使用 Firebase App Check? - Do I need a Firebase project to use Firebase App Check?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM