在Flutter中保存登录Google时出错 [英] Getting error while saving sign in google in Flutter

查看:58
本文介绍了在Flutter中保存登录Google时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习firebase身份验证和google登录.在这里,我准备了一个应用程序,将在其中进行google登录并保存登录数据,直到用户注销帐户为止.但是我遇到了错误,请解决此问题,我无法在任何地方找到它.

I am learning firebase auth and google sign in. Here, I have prepared an app in which I will do google sign in and save the sign-in data till users sign out the account. But I am getting an error please solve this I am not able to find it anywhere.

import 'package:CovidTracker/Auth/authentication.dart';
import 'package:CovidTracker/Auth/chatRoom.dart';
import 'package:CovidTracker/constraints.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

class SignInScreen extends StatefulWidget {
  @override
  _SignInScreenState createState() => _SignInScreenState();
}

Constraints color = new Constraints();

class _SignInScreenState extends State<SignInScreen> {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder<User>(
      future: FirebaseAuth.instance.currentUser,
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          User user = snapshot.data;
          return ChatRoom(uid: user.uid);
        } else {
          return Column(
            children: [
              SizedBox(
                height: 30.0,
              ),
              Container(
                height: 180,
                width: MediaQuery.of(context).size.width - 30,
                decoration: new BoxDecoration(
                  color: color.cardActiveBg,
                  borderRadius: new BorderRadius.all(Radius.circular(30.0)),
                ),
                child: Row(
                  children: [
                    Image.asset(
                      'assets/images/chat.png',
                      height: 170.0,
                    ),
                    Padding(
                      padding: const EdgeInsets.only(top: 30.0, left: 15.0),
                      child: Column(
                        children: [
                          Text(
                            'SignIn',
                            style: TextStyle(
                                fontWeight: FontWeight.bold,
                                fontFamily: 'Poppins',
                                fontSize: 28.0),
                          ),
                          Text(
                            'To Chat',
                            style: TextStyle(
                                fontWeight: FontWeight.bold,
                                fontFamily: 'Poppins',
                                fontSize: 30.0),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
              SizedBox(
                height: 20.0,
              ),
              Center(
                child: Image.asset(
                  'assets/images/googleLogo.png',
                  height: 100.0,
                ),
              ),
              SizedBox(
                height: 20.0,
              ),
              InkWell(
                onTap: () => googleSignInAcc().whenComplete(() async {
                  User user = await FirebaseAuth.instance.currentUser;
                  Navigator.of(context).pushReplacement(MaterialPageRoute(
                      builder: (_) => ChatRoom(uid: user.uid)));
                }),
                child: Container(
                  width: MediaQuery.of(context).size.width / 1.3,
                  height: 60.0,
                  decoration: new BoxDecoration(
                      borderRadius: new BorderRadius.circular(50.0),
                      color: color.primary),
                  child: Row(
                    children: [
                      SizedBox(
                        width: 20.0,
                      ),
                      Image.asset(
                        'assets/images/googleLogo.png',
                        height: 45.0,
                      ),
                      SizedBox(
                        width: 20.0,
                      ),
                      Text(
                        'Sign in with Google',
                        style: new TextStyle(
                          fontSize: 17.0,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          );
        }
      },
    );
  }
}

但是我在此行中遇到错误

      future: FirebaseAuth.instance.currentUser,

是说

The argument type 'User' can't be assigned to the parameter type 'Future<User>'.

Google登录代码在这里

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';

FirebaseAuth auth = FirebaseAuth.instance;
final googleSignIn = GoogleSignIn();

//error handling dialog box

showErrorDialog(BuildContext context, String err) {
  FocusScope.of(context).requestFocus(new FocusNode());

  return showDialog(
    context: context,
    child: AlertDialog(
      title: Text(
        "Error Occured",
      ),
      content: Text(err),
      actions: [
        OutlineButton(
          child: Text("OK"),
          onPressed: () => Navigator.pop(context),
        ),
      ],
    ),
  );
}

Future<bool> googleSignInAcc() async {
  GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();

  if (googleSignInAccount != null) {
    GoogleSignInAuthentication googleSignInAuthentication =
        await googleSignInAccount.authentication;

    AuthCredential credential = GoogleAuthProvider.credential(
        idToken: googleSignInAuthentication.idToken,
        accessToken: googleSignInAuthentication.accessToken);

    UserCredential result = await auth.signInWithCredential(credential);
    User user = await auth.currentUser;
    print(user.uid);
    return Future.value(true);
  }
}

Future<bool> signOutUser() async {
  User user = await auth.currentUser;
  await googleSignIn.disconnect();
  return Future.value(true);
}

希望这足以解释问题.请解决这个问题,我被困住了,解决问题的唯一方法就是通过你们.希望您能帮助学习这个新手.非常感谢(:)

Hope this is enough to explain the question. Please solve this I am stuck and the only way to get it solved is through you guys. Hope you will help to learn this newbie. Thank You So Much(:)

推荐答案

当我编写未来的构造函数时.我总是使用AsyncSnapshot类.

When I program a future constructor. I always use an AsyncSnapshot class.

在您的代码中:

builder: (context, snapshot) {
    if (snapshot.hasData) {
      User user = snapshot.data;

尝试:

builder: (context, AsyncSnaphot<User> snapshot) {
    if (snapshot.hasData) {
      User user = snapshot.data;

这篇关于在Flutter中保存登录Google时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆