繁体   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