无法在 Flutter 中使用谷歌登录

时间:2021-04-15 12:23:12

标签: flutter dart

我可以使用我的 flutter 应用程序的网络版本进行 Google 登录,但无法通过 Android 应用程序进行 Google 登录。 这是我得到的错误:

 GraphicExtModuleLoader::CreateGraphicExtInstance false
D/Surface (29460): Surface::connect(this=0x753ab3e000,api=1)
D/Surface (29460): Surface::setBufferCount(this=0x753ab3e000,bufferCount=3)
D/Surface (29460): Surface::allocateBuffers(this=0x753ab3e000)
V/PhoneWindow(29460): DecorView setVisiblity: visibility = 0, Parent = android.view.ViewRootImpl@9837577, this = DecorView@bcd75e4[MainActivity]
V/PhoneWindow(29460): DecorView setVisiblity: visibility = 4, Parent = android.view.ViewRootImpl@9de9d56, this = DecorView@63bb4f5[SignInHubActivity]
D/Surface (29460): Surface::disconnect(this=0x753ab3e000,api=1)
D/View    (29460): [Warning] assignParent to null: this = DecorView@63bb4f5[SignInHubActivity]
E/flutter (29460): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null, null)

参考 StackOverflow 上的这个 Flutter and google_sign_in plugin: PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null) 答案,我不明白这个答案想说什么...... 但是,我确实在 firebase 上提供了我的 SHA1 密钥。重新下载并替换了我的 flutter 应用中的 google-services.json,但仍然无法在 Android 上登录。 这是我的 GoogleSignIn 代码:

onPressed: () async {

                              await Firebase.initializeApp();

                              final FirebaseAuth _firebaseAuth =
                                  FirebaseAuth.instance;
                              final GoogleSignIn _googleSignIn =
                              GoogleSignIn();

                              Future<User> _signIn(BuildContext context) async {
                                debugPrint("1");
                                final GoogleSignInAccount googleUser =
                                await _googleSignIn.signIn();
                                debugPrint("2");
                                final GoogleSignInAuthentication googleAuth =
                                await googleUser.authentication;
                                debugPrint("a");
                                final AuthCredential credential =
                                GoogleAuthProvider.credential(
                                    idToken: googleAuth.idToken,
                                    accessToken: googleAuth.accessToken);

                                User userDetails = (await _firebaseAuth
                                    .signInWithCredential(credential))
                                    .user;
                                ProviderDetails providerInfo =
                                ProviderDetails(userDetails.uid);

                                List<ProviderDetails> providerData =
                                <ProviderDetails>[];
                                providerData.add(providerInfo);

                                UserDetails details = UserDetails(
                                    userDetails.uid,
                                    userDetails.displayName,
                                    userDetails.email,
                                    userDetails.photoURL,
                                    providerData);

                                if (details.userName.toString() != '') {
                                  debugPrint("Email ${details.userEmail}");

                                  globals.isLoggedIn = true;
                                  SharedPref prefs = SharedPref();
                                  String photoUrl = details.photoUrl.toString();
                                  prefs.save("photoUrl", photoUrl);

                                  prefs.save("username",
                                      details.userName.toString());
                                  prefs.save(
                                      "email", details.userEmail.toString());
                                  if (mounted) {
                                    setState(() {
                                      inProgress = false;
                                    });
                                  }
                                } else {
                                  globals.isLoggedIn = false;
                                  debugPrint(
                                      "Check your internet Connection");
                                }
                              }

                              if (mounted) {
                                setState(() {
                                  inProgress = true;
                                });
                              }
                              await _signIn(context);
                              debugPrint("LoggedIn");
                              Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) => Dashboard()),
                              );
                            }

1 个答案:

答案 0 :(得分:0)

实际上我需要将此行添加到我的应用程序级 build.gradle 文件依赖项部分。

 implementation 'com.google.android.gms:play-services-auth:19.0.0'

也需要从此链接链接我的项目。

https://developers.google.com/identity/sign-in/android/start-integrating#configure_a_project

将来可能会帮助某人!