在我的 Flutter 应用程序中,我曾经使用 Google 登录,工作正常,一两个月后我再次测试它,它不再工作了,我试图找出如何以及为什么,但我不断收到此消息甚至不特定于某事物的消息。是的,我在 firebase 和 google console 中有相同的 SHA-1 是的,我在正确的位置更新了 google-service.json 。 (另请注意,我在 firebase 和 google cloud 中有一个最近(不到 30 天)删除的项目,具有相同的 SHA-1 密钥和包名称)
这是相关代码 终端
I/flutter (24662): Initializing Firebase
I/flutter (24662): Firebase initialized
....
**E/flutter (24662): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(exception, Unknown, null, null)**
E/flutter (24662): #0 GoogleSignInApi.getAccessToken (package:google_sign_in_android/src/messages.g.dart:250:7)
E/flutter (24662): <asynchronous suspension>
E/flutter (24662): #1 GoogleSignInAndroid.getTokens.<anonymous closure> (package:google_sign_in_android/google_sign_in_android.dart:68:15)
E/flutter (24662): <asynchronous suspension>
E/flutter (24662): #2 GoogleSignInAccount.authentication (package:google_sign_in/google_sign_in.dart:95:9)
E/flutter (24662): <asynchronous suspension>
E/flutter (24662): #3 _LoginSignupPageState.signInWithGoogle (package:flutter_drive_advisor/screens/login_page.dart:66:51)
E/flutter (24662): <asynchronous suspension>
E/flutter (24662): #4 _LoginSignupPageState.build.<anonymous closure> (package:flutter_drive_advisor/screens/login_page.dart:210:68)
E/flutter (24662): <asynchronous suspension>
登录页面.dart
Future<UserCredential?> signInWithGoogle() async {
// Trigger the authentication flow
final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();
// Obtain the auth details from the request
final GoogleSignInAuthentication googleAuth = await googleUser!.authentication;
// Create a new credential
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
UserCredential userCredential = await _auth.signInWithCredential(credential);
// Save the user's ID to SharedPreferences
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString('userId', userCredential.user!.uid);
// Once signed in, return the UserCredential
return userCredential;
}
pubspec.yaml
dependencies:
flutter:
sdk: flutter
http: ^0.13.3
google_maps_flutter: ^2.0.10
autocomplete_textfield: ^2.0.1
flutter_typeahead: ^3.2.1
flutter_native_splash: ^1.2.1
google_fonts: ^4.0.4
shared_preferences: ^2.0.8
firebase_auth: ^4.19.2 #//4.17.8
google_sign_in: ^6.2.1
flutter_facebook_auth: ^3.5.6
permission_handler: ^8.1.6
url_launcher: ^6.0.10
cloud_firestore: ^4.15.8
location: ^4.1.1 #^4.4.0
android_intent: ^2.0.2 # Add this line
app_settings: ^4.1.1
optimize_battery: ^0.0.4
intl: ^0.17.0
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.6
firebase_core: ^2.30.0 #//2.27.0
collection: ^1.18.0
flutter_local_notifications: ^8.1.1+1
我尝试过的:
确保您已在 Firebase 控制台中为您的项目正确设置 Google 登录方法。 确保您的 Android 应用的 SHA-1 证书指纹已正确添加到 Firebase 控制台中。您可以通过在 Android 应用程序目录中运行命令 ./gradlewsigningReport 来获取 SHA-1 指纹。 检查 google-services.json 文件是否放置在正确的位置,该位置应该是 Flutter 项目的 android/app 目录。 确保您的 pubspec.yaml 文件中包含最新版本的 google_sign_in 和 firebase_core 包。 尝试运行 flutter clean,然后 flutter run 来清理构建缓存并再次运行应用程序。
你的 android/build.gradle 有以下代码吗?
allprojects {
repositories {
google() // <- this one
mavenCentral()
}
}
关于如何在 flutter 项目中编写 android/app/build.gradle 和 android/build.gradle 文件进行了一些更新。检查 flutter 网站并进行相应迁移。希望这能解决您的问题。