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

Time:09-08

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                     
                                ),
                              ),
                             }
                          }
  

CodePudding user response:

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 :)

  • Related