用 flutter 在 firebase 中取名

getting name in firebase with flutter

我在尝试学习 flutter 和 firebase 时遇到问题。我正在尝试访问 Google 帐户的名称,但我无法访问,因为类型不兼容。 myHomePage 只是我的应用程序的另一个页面。

这是有问题的代码段。

class _BodyState extends State<Body> {
  User user ;

  void click() {
    signInWithGoogle().then((user) => {
      this.user = user as User,
      Navigator.push(context,
    MaterialPageRoute(builder: (context) => MyHomePage(this.user.displayName)))
    });
  }

  Widget googleLoginButton(){
    return OutlineButton(
        onPressed: this.click,
      shape : RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(45)),
      splashColor: Colors.grey,
      borderSide: BorderSide(color: Colors.grey),
      child: Padding(padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Image(image: AssetImage('assets/google_logo.png'), height: 35),
          Padding(
              padding: EdgeInsets.only(left: 10),
              child: Text('Sign in with Google',
                  style: TextStyle(color: Colors.grey,
                      fontSize: 25)))],
      ))

    );
  }
  @override
  Widget build(BuildContext context) {
    return Align(
        alignment: Alignment.center,
        child: Padding(
            padding: EdgeInsets.all(10),
            child: TextField(
              decoration: InputDecoration(
                prefixIcon: Icon(Icons.person),
                labelText: "Type your name : ",
                border: OutlineInputBorder(
                    borderSide: BorderSide(width: 5, color: Colors.black)),
                suffixIcon: IconButton(
                  icon: Icon(Icons.done),
                  splashColor: Colors.orange,
                  tooltip: "Submit",
                  onPressed: this.click,
                ),
              ),
            )));
  }
}

它将显示的错误是:

Unhandled Exception: type 'String' is not a subtype of type 'User'

我知道类型不兼容,但您可以看到下面的代码(错误的主要部分):

class _BodyState extends State<Body> {
      User user ;
    
      void click() {
        signInWithGoogle().then((user) => {
          this.user = user as User,
          Navigator.push(context,
        MaterialPageRoute(builder: (context) => MyHomePage(this.user.displayName)))
        });
      }

我试了很多次,也找了一些其他的教程,但还是无法解决这个问题。
then 方法中 signInWithGoogle 之后的 user 是一个字符串,其中包含用户的信息。我想问一下如何访问 Google 帐户的名称?

FlutterFire example returns UserCredential 未来中的 signInWithGoogle() 函数,它有一个用户 属性。尝试:

signInWithGoogle().then((userCredential) => {
      this.user = userCredential.user;
      Navigator.push(context,
    MaterialPageRoute(builder: (context) => MyHomePage(this.user.displayName)))
    });