gpt4 book ai didi

firebase - flutter firebase google登录以及如何从多个类访问

转载 作者:IT王子 更新时间:2023-10-29 07:05:36 24 4
gpt4 key购买 nike

我有一个可用的 google 登录和注销从教程中学习

但我不知道如何从另一个类(class)访问它。我想要从登录屏幕到主屏幕的用户个人资料图片。

_googleSignIn.signIn().then((result) {
result.authentication.then((googleKey) {
FirebaseAuth.instance
.signInWithGoogle(
idToken: googleKey.idToken,
accessToken: googleKey.accessToken)
.then((signedInUser) {
print(
'Signed in as ${signedInUser.displayName} ${signedInUser.photoUrl}');
widget.onSignIn();
}).catchError((e) {
print(e);
}).catchError((e) {
print(e);
}).catchError((e) {
print(e);
});
});
});

这是我的登录代码我想从另一个类访问 signedInUser.displayName 以及 signedInUser.photourl

最佳答案

在第二类中获取当前用户的一种方法是使用身份验证状态监听器。最简单的方法是:

FirebaseAuth.instance.onAuthStateChanged.listen((user) {
print(user);
});

只要身份验证状态发生变化,就会触发此监听回调,您可以使用它来读取用户的属性(或更新 UI 以反射(reflect)身份验证状态)。

您还可以确保在第二类中进行身份验证(复制您现在在第一类中所做的部分工作),或者使用共享首选项传递数据。有关所有三种方法的示例,请参阅 Firebase Login with Flutter using onAuthStateChanged .

关于firebase - flutter firebase google登录以及如何从多个类访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53454393/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com