Implementig google sign in help!

Dear :wink: my goal is to log in with google …and take image of user and store in firebase …
so… but i have an error during log in …someone help me’?? i attach code an error…

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

               import '../main.dart';

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

                      Future<String> signInWithGoogle() async {
                      final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
                   final GoogleSignInAuthentication googleSignInAuthentication =
                 await googleSignInAccount.authentication;

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

            final AuthResult authResult = await _auth.signInWithCredential(credential);
           final FirebaseUser user = authResult.user;

               assert(!user.isAnonymous);
                assert(await user.getIdToken() != null);

                     final FirebaseUser currentUser = await _auth.currentUser();
                      assert(user.uid == currentUser.uid);

                      print('userlogin');
                      }

                  void signOutGoogle() async{
                    await googleSignIn.signOut();

                   print("User Sign Out");
                     }

                             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


                           void checkUser()
                                 {
                                   //Check if the user is signned in or not with the currentUser() function
                               if(auth.currentUser() != null)
                                      {

                                   var userImageUrl = user.photoUrl;
                      //if the user is signned in then set the url to be the image url
                     print(userImageUrl);
                     print('loggato');

                          
                       }
                       else
                        {
                      //call signin method to make the user signin
                      print('errore');
                       }
                       }


                   class LoginPage extends StatefulWidget {
                     @override
                      _LoginPageState createState() => _LoginPageState();
                           }

                          class _LoginPageState extends State<LoginPage> {
                   @override
                          Widget build(BuildContext context) {
                          return Scaffold(
                                body: Container(
                                 color: Colors.white,
                            child: Center(
                           child: Column(
                           mainAxisSize: MainAxisSize.max,
                       mainAxisAlignment: MainAxisAlignment.center,
                              children: <Widget>[
                      FlutterLogo(size: 150),
          SizedBox(height: 50),
          _signInButton(),
          _signout(),
        ],
                     ),
                  ),
               ),
             );
             }

             Widget _signInButton() {
                  return OutlineButton(
                       splashColor: Colors.grey,
                              onPressed: () {
                                signInWithGoogle().whenComplete(() {

                               checkUser();
                               Navigator.push(
                               context,
                             MaterialPageRoute(builder: (context) => FirstScreen()),
                                );
                                },
                                  });
                                     },
                            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(40)),
                               highlightElevation: 0,
                           borderSide: BorderSide(color: Colors.grey),
                       child: Padding(
                    padding: const EdgeInsets.fromLTRB(0, 10, 0, 10),
                 child: Row(
                 mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Image(image: AssetImage("assets/googlelogo.png"), height: 35.0),
        Padding(
          padding: const EdgeInsets.only(left: 10),
          child: Text(
            'Sign in with Google',
            style: TextStyle(
              fontSize: 20,
              color: Colors.grey,
                      ),
                          ),
                    )
                     ],
                  ),
                ),
             );
           }
            Widget _signout() {
              return OutlineButton(
                 splashColor: Colors.grey,
                  onPressed: () {


                        signOutGoogle();
                     Navigator.push(
                    context,
                  MaterialPageRoute(builder: (context) => FirstScreen()),
                   );
                  },

                         },
                      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(40)),
                   highlightElevation: 0,
                   borderSide: BorderSide(color: Colors.grey),
                     child: Padding(
                  padding: const EdgeInsets.fromLTRB(0, 10, 0, 10),
    child: Row(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Image(image: AssetImage("assets/googlelogo.png"), height: 35.0),
        Padding(
          padding: const EdgeInsets.only(left: 10),
          child: Text(
            'Sign out with Google',
            style: TextStyle(
              fontSize: 20,
              color: Colors.grey,
                        ),
                       ),
                      )
                     ],
                    ),
                  ),
                  );
                }
                 }

Error is …

E/flutter (11994): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The method ‘currentUser’ was called on null.
E/flutter (11994): Receiver: null
E/flutter (11994): Tried calling: currentUser()
E/flutter (11994): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
E/flutter (11994): #1 checkUser (package:csf_2020/pages/login_page.dart:49:11)
E/flutter (11994): #2 _LoginPageState._signInButton.. (package:csf_2020/pages/login_page.dart:102:9)
E/flutter (11994): #3 _rootRun (dart:async/zone.dart:1122:38)
E/flutter (11994): #4 _CustomZone.run (dart:async/zone.dart:1023:19)
E/flutter (11994): #5 _FutureListener.handleWhenComplete (dart:async/future_impl.dart:163:18)
E/flutter (11994): #6 Future._propagateToListeners.handleWhenCompleteCallback (dart:async/future_impl.dart:650:39)
E/flutter (11994): #7 Future._propagateToListeners (dart:async/future_impl.dart:706:37)
E/flutter (11994): #8 Future._completeError (dart:async/future_impl.dart:532:5)
E/flutter (11994): #9 _AsyncAwaitCompleter.completeError (dart:async-patch/async_patch.dart:38:15)
E/flutter (11994): #10 signInWithGoogle (package:csf_2020/pages/login_page.dart)
E/flutter (11994):
E/flutter (11994): #11 _LoginPageState._signInButton. (package:csf_2020/pages/login_page.dart:100:9)
E/flutter (11994): #12 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:706:14)
E/flutter (11994): #13 _InkResponseState.build. (package:flutter/src/material/ink_well.dart:789:36)
E/flutter (11994): #14 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter (11994): #15 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:486:11)
E/flutter (11994): #16 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:264:5)
E/flutter (11994): #17 BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:199:7)
E/flutter (11994): #18 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:467:9)
E/flutter (11994): #19 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:76:12)
E/flutter (11994): #20 PointerRouter._dispatchEventToRoutes. (package:flutter/src/gestures/pointer_router.dart:117:9)
E/flutter (11994): #21 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:379:8)
E/flutter (11994): #22 PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:115:18)
E/flutter (11994): #23 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:7)
E/flutter (11994): #24 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:19)
E/flutter (11994): #25 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter (11994): #26 GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter (11994): #27 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter (11994): #28 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter (11994): #29 _rootRunUnary (dart:async/zone.dart:1138:13)
E/flutter (11994): #30 _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter (11994): #31 _CustomZone.runUnaryGuarded (dart:async/zone.dart:933:7)
E/flutter (11994): #32 _invoke1 (dart:ui/hooks.dart:273:10)
E/flutter (11994): #33 _dispatchPointerDataPacket (dart:ui/hooks.dart:182:5)
E/flutter (11994):

Thanks, I try to implement your code. If you need development you can go here:
Flutter Developers

Please try this tutorial. It helped me implement Google Sign in successfully! https://medium.com/flutter-community/flutter-implementing-google-sign-in-71888bca24ed

@daniele777 This is most likely because you may have missed the step of adding an SHA key in your firebase App.

Make sure you generate both SHA1 and SHA256 keys and add them to your project on firebase.

1 Like

Thanks for reply i’m still try to solve problem semms i can’t take image from google profile…but i need to use in marker…