簡體   English   中英

Google Flutter Firebase登錄以及如何從多個類訪問

[英]flutter firebase google sign in and how to access from multiple classes

我有一個工作正常的Google登錄並注銷,從本教程中學習

但我不知道如何從另一個班級訪問它。 我希望用戶個人資料圖片從登錄屏幕轉到主屏幕。

_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.photourl訪問signedInUser.displayName

在第二類中選擇當前用戶的一種方法是使用auth狀態偵聽器。 最簡單的方法是:

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

每當身份驗證狀態更改時,都會觸發此偵聽回調,您可以使用它從用戶讀取屬性(或更新UI以反映身份驗證狀態)。

您還可以確保在第二堂課中進行身份驗證(復制您現在在第一堂課中所做的部分工作),或使用共享首選項傳遞數據。 有關這三種方法的示例,請參閱使用onAuthStateChanged使用Flutter登錄Firebase

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM