0
votes
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'home.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_auth/firebase_auth.dart';

class Login extends StatefulWidget {
  @override
  _LoginState createState() => _LoginState();
}

class _LoginState extends State<Login> {
  final GoogleSignIn googleSignIn = new GoogleSignIn();
  final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
  SharedPreferences preferences;
  bool loading = false;
  bool isLogedin = false;

  @override
  void initState() {
    super.initState();
    isSignedIn();
  }

  void isSignedIn() async {
    WidgetsFlutterBinding.ensureInitialized();
    await Firebase.initializeApp();
    setState(() {
      loading = true;
    });
preferences = await SharedPreferences.getInstance();
isLogedin = await googleSignIn.isSignedIn();
if (isLogedin) {
  Navigator.pushReplacement(
      context, MaterialPageRoute(builder: (context) => new HomePage()));
}

setState(() {
  loading = false;
});


}



Future handleSignIn() async {
    preferences = await SharedPreferences.getInstance();
    setState(() {
      loading = true;
    });

GoogleSignInAccount googleUser = await googleSignIn.signIn();
GoogleSignInAuthentication googleSignInAuthentication =
    await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.credential(
  accessToken: googleAuth.accessToken,
  idToken: googleAuth.idToken,
);
if (UserCredential != null) {
  final QuerySnapshot result = await FirebaseFirestore.instance
      .collection("users")
      .where("id", isEqualTo: UserCredential.uid)
      .get();
  final List<DocumentSnapshot> documents = result.documents;
  if (documents.length == 0) {
    //insert the user to our collection
    FirebaseFirestore.instance
        .collection("users")
        .document(UserCredential.uid)
        .setData({
      "id": UserCredential.uid,
      "username": UserCredential.displayName,
      "profilePicture": UserCredential.photoUrl
    });

    await preferences.setString("id", UserCredential.uid);
    await preferences.setString("username", UserCredential.displayName);
    await preferences.setString("photoUrl", UserCredential.displayName);
  } else {
    await preferences.setString("id", documents[0]['id']);
    await preferences.setString("username", documents[0]['username']);
    await preferences.setString("photoUrl", documents[0]['photoUrl']);
  }

  Fluttertoast.showToast(msg: "Başarıyla Giriş Yapıldı");
  setState(() {
    loading = false;
  });
  // This is the missing Code
  Navigator.pushReplacement(
      context, MaterialPageRoute(builder: (context) => HomePage()));
} else {
  //Missing Code
  Fluttertoast.showToast(msg: "Login failed");
}

}

//-----------Paste from here ------------------
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        centerTitle: true,
        title: new Text(
          "Login",
          style: TextStyle(color: Colors.red.shade900),
        ),
      ),
      body: Stack(
        children: <Widget>[
          Visibility(
              visible: loading ?? true,
              child: Center(
                child: Container(
                  alignment: Alignment.center,
                  color: Colors.white.withOpacity(0.9),
                  child: CircularProgressIndicator(
                    valueColor: AlwaysStoppedAnimation<Color>(Colors.red),
                  ),
                ),
              ))
        ],
      ),
      bottomNavigationBar: Container(
        child: Padding(
          padding: const EdgeInsets.only(
              left: 12.0, right: 12.0, top: 8.0, bottom: 8.0),
          child: FlatButton(
              color: Colors.red,
              onPressed: () {
                //handleSignIn();
              },
              child: Text(
                "Sign in / Sign up with google",
                style: TextStyle(color: Colors.white),
              )),
        ),
      ),
    );
  }
}

MY ERRORSS !!================================================ Performing hot reload... Syncing files to device sdk gphone x86... lib/pages/login.dart:70:50: Error: Getter not found: 'uid'. .where("id", isEqualTo: UserCredential.uid) ^^^ lib/pages/login.dart:77:38: Error: Getter not found: 'uid'. .document(UserCredential.uid) ^^^ lib/pages/login.dart:79:32: Error: Getter not found: 'uid'. "id": UserCredential.uid, ^^^ lib/pages/login.dart:80:38: Error: Getter not found: 'displayName'. "username": UserCredential.displayName, ^^^^^^^^^^^ lib/pages/login.dart:81:44: Error: Getter not found: 'photoUrl'. "profilePicture": UserCredential.photoUrl ^^^^^^^^ lib/pages/login.dart:84:58: Error: Getter not found: 'uid'. await preferences.setString("id", UserCredential.uid); ^^^ lib/pages/login.dart:85:64: Error: Getter not found: 'displayName'. await preferences.setString("username", UserCredential.displayName); ^^^^^^^^^^^ lib/pages/login.dart:86:64: Error: Getter not found: 'displayName'. await preferences.setString("photoUrl", UserCredential.displayName); ^^^^^^^^^^^ lib/pages/login.dart:64:20: Error: The getter 'googleAuth' isn't defined for the class '_LoginState'.

  • '_LoginState' is from 'package:ecommerce_app/pages/login.dart' ('lib/pages/login.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'googleAuth'. accessToken: googleAuth.accessToken, ^^^^^^^^^^ lib/pages/login.dart:65:16: Error: The getter 'googleAuth' isn't defined for the class '_LoginState'.
  • '_LoginState' is from 'package:ecommerce_app/pages/login.dart' ('lib/pages/login.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'googleAuth'. idToken: googleAuth.idToken, ^^^^^^^^^^ lib/pages/login.dart:72:55: Error: The getter 'documents' isn't defined for the class 'QuerySnapshot'.
  • 'QuerySnapshot' is from 'package:cloud_firestore/cloud_firestore.dart' ('/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.16.0/lib/cloud_firestore.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'documents'. final List documents = result.documents; ^^^^^^^^^ lib/pages/login.dart:77:14: Error: The method 'document' isn't defined for the class 'CollectionReference'.
  • 'CollectionReference' is from 'package:cloud_firestore/cloud_firestore.dart' ('/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.16.0/lib/cloud_firestore.dart'). Try correcting the name to the name of an existing method, or defining a method named 'document'. .document(UserCredential.uid) ^^^^^^^^
1

1 Answers

0
votes

You're getting the error because googleAuth is not defined and looking at your code, accessToken and idToken are properties of the GoogleSignInAuthentication object which you already created here:

GoogleSignInAuthentication googleSignInAuthentication =
    await googleUser.authentication;

So you just need to change googleAuth to googleSignInAuthentication.

Your code should change from :

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

to:

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