[英]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.