简体   繁体   中英

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

I want to get the snapShot of user provider If the user have sign in with Email show this. But if user have sign in with Gmail then show this.

code from what I get so far:

           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 is type of List<UserInfo> , and each UserInfo class has a getter providerId . For sure, that's what you're looking for too.

So what you can do is

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

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

And based on these bools, show your widgets.

Hope this helps:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM