簡體   English   中英

如果用戶在 flutter 中使用 email 或 gmail 登錄,如何獲取快照

[英]How to get snapShot if user have sign in with email or gmail in flutter

我想獲取用戶提供者的快照 如果用戶使用 Email 登錄,請顯示此內容。 但如果用戶使用 Gmail 登錄,則顯示此內容。

到目前為止我得到的代碼:

           StreamBuilder<User?>(
                stream: authBloc.currentUser,
                builder: (context, snapshot) {
                  if (!snapshot.hasData) {
                    return const Text(
                        'Oops somethings went wrong');
                          }    
                        body: ...(
                         snapshot.data!.providerData.contains("email")
                              ? Text("You have sign in with Email")
                                //Show this if user have sign In with Email provider   
                                                    
                              : Text("You have sign in with Gmail"),
                                //Show this if user have sign In with Gmail provider                     
                                ),
                              ),
                             }
                          }
  

User.providerData 是List<UserInfo>的類型,每個 UserInfo class 都有一個 getter providerId 當然,這也是您正在尋找的。

所以你能做的是

bool isEmailPasswordUser = snapshot.data!.providerData.any(
    (userInfo) => userInfo.providerId == "password",
);

bool isGoogleAuthUser = snapshot.data!.providerData.any(
    (userInfo) => userInfo.providerId == "google.com",
);

並基於這些布爾值,顯示您的小部件。

希望這可以幫助:)

暫無
暫無

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

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