首页 > 解决方案 > 谷歌登录 - Flutter

问题描述

使用 GoogleSignIn 插件后,如何设置标准头像,当登录 Google 时,它​​会更改为 Google PhotoURL?我可以很好地使用 _handlesignin 函数,但登录时无法更改状态?(我在尝试根据登录状态创建登录/注销按钮时遇到类似的问题)

我认为这将是某种类型的 if 函数,然后是 else,但无法让它工作。

标签: dartfluttergoogle-signingooglesigninaccount

解决方案


是的,你是对的,它需要一些 if else 语句。我认为您正在寻找auth.currentUser()函数来检查用户的登录和注销状态。

以下代码检查用户登录状态,如果用户已登录,则放置用户个人资料照片。

  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();
    }
  }

推荐阅读