dartfluttergoogle-signingooglesigninaccount

Google sign in - Flutter


After using the GoogleSignIn plugin, how do I go about setting a standard avatar, and when signed into Google it changes to the Google PhotoURL? I can use the _handlesignin function fine, but cant get the state to change upon signing in? (I have similar problem with trying to create a sign in/sign out button based on the state of the sign in)

I presume it would be some type of if function then else, but couldn't get it to work.


Solution

  • Yes you are right that it requires some if else statement. I think you are looking for auth.currentUser() function to check the state of the signin and singout of the user.

    The following code checks the user signin status and if the user is signned in then places the user profile photo.

      FirebaseAuth auth; //firebase auth
      FirebaseUser user; // firebase user
    
      var imageUrl = "assets/image.png";  //you can use a image 
      //as a default image that would be replaced later with the profile photo
    
    
      Widget userProfilePhoto()
      {
        return Center(
            child: Container(
              height: 100.0,
              width: 100.0,
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                image: DecorationImage(
                  fit : BoxFit.fill,
                  image: NetworkImage(userurl)
                )
              ), 
            )
          ),
      }
    
    
      void checkUser()
      {
        //Check if the user is signned in or not with the currentUser() function
        if(auth.currentUser() != null)
        {
          setState((){
            userImageUrl = user.photoUrl;
            //if the user is signned in then set the url to be the image url
          });
        }
        else
        {
          //call signin method to make the user signin
          signIn();
        }
      }