1

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

But I am getting error in this line

      future: FirebaseAuth.instance.currentUser,

It is saying that

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

Google sign-in code is here

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

Shivam Yadav
  • 47
  • 1
  • 1
  • 6

1 Answers1

0

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

in your code:

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

try:

builder: (context, AsyncSnaphot<User> snapshot) {
    if (snapshot.hasData) {
      User user = snapshot.data;
  • That's ok but **how can I use** `future: FirebaseAuth.instance.currentUser,` as I am getting an error in this line. Can you please help in doing so? – Shîvam Yadav Sep 17 '20 at 12:19