Flutter Google Sign In - Email information is not coming to Firebase (returns blank)

Viewed 453

When I log in with google in the application I made on Flutter, the email returns to firebase as a null value. I do not get any errors in the runtime or login. I made changes to the google_sign_in version on the pubspec.yaml file, but the result has not changed.

user_repository.dart :

    import 'package:pisipisikolog/locator.dart';
    import 'package:pisipisikolog/model/user_model.dart';
    import 'package:pisipisikolog/services/auth_base.dart';
    import 'package:pisipisikolog/services/fake_auth_service.dart';
    import 'package:pisipisikolog/services/firebase_auth_service.dart';
    import 'package:pisipisikolog/services/firestore_db_service.dart';

    enum AppMode {DEBUG, RELEASE}

    class UserRepository implements AuthBase{

    FirebaseAuthService _firebaseAuthService = locator<FirebaseAuthService>();
    FakeAuthenticationService _fakeAuthenticationService = locator<FakeAuthenticationService>();
    FirestoreDbService _firestoreDbService = locator<FirestoreDbService>();

    AppMode appMode = AppMode.RELEASE;
 
              @override
      Future<User> currentUser() async{
        if (appMode == AppMode.DEBUG) {
          return await _fakeAuthenticationService.currentUser();
        } else {
          return await _firebaseAuthService.currentUser();
        }
      }

      @override
      Future<User> signInAnonymously()  async{
        if (appMode == AppMode.DEBUG) {
          return await _fakeAuthenticationService.signInAnonymously();
        } else {
          return await _firebaseAuthService.signInAnonymously();
        }
      }

      @override
      Future<bool> signOut() async{
        if (appMode == AppMode.DEBUG){
          return await _firebaseAuthService.signOut();
        } else {
          return await _firebaseAuthService.signOut();
        }
      }

      @override
      Future<User> signInWithGoogle() async{
        if (appMode == AppMode.DEBUG) {
          return await _fakeAuthenticationService.signInWithGoogle();
        } else {
          User _user = await _firebaseAuthService.signInWithGoogle();
          bool _sonuc = await _firestoreDbService.saveUser(_user);
          if (_sonuc) {
            return _user; 
          }else return null;
          }
      }

      @override
      Future<User> signInWithFacebook() async{
        if (appMode == AppMode.DEBUG) {
          return await _fakeAuthenticationService.signInWithFacebook();
        } else {
          User _user = await _firebaseAuthService.signInWithFacebook();
          bool _sonuc = await _firestoreDbService.saveUser(_user);
          if (_sonuc) {
            return _user; 
          }else return null;
          }
      }

      @override
      Future<User> createUserWithEmailandPassword(String email, String sifre) async{
          if (appMode == AppMode.DEBUG) {
          return await _fakeAuthenticationService.createUserWithEmailandPassword(email, sifre);
        } else {
          User _user = await _firebaseAuthService.createUserWithEmailandPassword(email, sifre);
          bool _sonuc = await _firestoreDbService.saveUser(_user);
          if (_sonuc) {
            return _user; 
          }else return null;
          }
          
        }

      @override
      Future<User> signInWithEmailandPassword(String email, String sifre) async{
        if (appMode == AppMode.DEBUG) {
          return await _fakeAuthenticationService.signInWithEmailandPassword(email, sifre);
        } else {
          return await _firebaseAuthService.signInWithEmailandPassword(email, sifre);
        }
      }

    }

user_model.dart :

    import 'package:cloud_firestore/cloud_firestore.dart';
    import 'package:flutter/material.dart';
    import 'package:flutter/cupertino.dart';

    class User {
    final String userID;
    String email;
    String userName;
    String profilURL;
    DateTime createdAt;
    DateTime updatedAt;
    int seviye;

    User({@required this.userID, @required this.email});

    Map<String, dynamic> toMap(){
      return {
        "UserID" : userID,
        "email" : email,
        "userName" : userName ?? '',
        "profilURL" : profilURL ?? '',
        "createdAt" : createdAt ?? FieldValue.serverTimestamp(),
        "updatedAt" : updatedAt ?? FieldValue.serverTimestamp(),
        "seviye" : seviye ?? 1,
      };
    }

    User.fromMap(Map<String, dynamic> map):
      userID = map['userID'],
      email = map['email'],
      userName = map['userName'],
      profilURL = map['profilURL'],
      createdAt = (map['createdAt'] as Timestamp).toDate(),
      updatedAt = (map['updatedAt']as Timestamp).toDate(),
      seviye = map['seviye'];

    @override
    String toString() {
      return 'User{userID: $userID, email: $email, userName: $userName, profilURL: $profilURL, createdAt: $createdAt, updatedAt: $updatedAt, seviye: $seviye}';
    }


    }

Screenshot

3 Answers

You can try something like this to update Email in Firebase

    @override
  Future<User> signInWithGoogle() async {
    GoogleSignIn _googleSignIn = GoogleSignIn();
    GoogleSignInAccount _googleUser = await _googleSignIn.signIn();
    String _googleUserEmail = _googleUser.email; //Get user email.
    if (_googleUser != null) {
      GoogleSignInAuthentication _googleAuth = await _googleUser.authentication;
      if (_googleAuth.idToken != null && _googleAuth.accessToken != null) {
        AuthResult sonuc = await _firebaseAuth.signInWithCredential(
            GoogleAuthProvider.getCredential(
                idToken: _googleAuth.idToken,
                accessToken: _googleAuth.accessToken));
        FirebaseUser _user = sonuc.user;
        sonuc.user.updateEmail(_googleUserEmail);
        return _userFromFirebase(_user);
      } else {
        return null;
      }
    } else {
      return null;
    }
  }

That's because this user is not verified in Google. You can ask verify this email to continue using your app. Or ask it to sign up with login and password.

You may try this way to get email address.

final User? user = (await _auth.signInWithCredential(credential)).user;
String email = user.providerData[0].email;
Related