我尝试在 Android 上的应用程序中通过 Google 登录,当它调用
await GoogleSignIn().signIn();
时,我的应用程序崩溃了。
这是登录小部件的代码:
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:shared_preferences/shared_preferences.dart';
class Signin extends StatelessWidget {
const Signin({super.key, required this.setIsSigned});
final setIsSigned;
void signin () async {
final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn(); // crashes here
final GoogleSignInAuthentication? googleAuth = await googleUser?.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth?.accessToken,
idToken: googleAuth?.idToken,
);
// Once signed in, return the UserCredential
final userCredential = await FirebaseAuth.instance.signInWithCredential(credential);
final email = userCredential.user?.email;
final login = email?.split('@')[0];
DatabaseReference ref = FirebaseDatabase.instance.ref('/users/$login');
final snap = await ref.get();
final data = snap.value;
if (data == null){
await ref.set({
"email": email,
});
}
final SharedPreferences prefs = await SharedPreferences.getInstance();
if (login != null){
await prefs.setString('login', login);
}
setIsSigned(true, login);
}
@override
Widget build(BuildContext context){
return Align(
alignment: Alignment.center,
child: ElevatedButton(
onPressed: signin,
child: const Text('Signin via Google'),
)
);
}
}
调试控制台中的错误:
D/AndroidRuntime(31316): Shutting down VM
E/AndroidRuntime(31316): FATAL EXCEPTION: main
E/AndroidRuntime(31316): Process: com.ms.sndst, PID: 31316
E/AndroidRuntime(31316): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=53293, result=-1, data=Intent { (has extras) }} to activity {com.ms.sndst/com.ms.sndst.MainActivity}: kotlin.UninitializedPropertyAccessException: lateinit property mresult has not been initialized
E/AndroidRuntime(31316): at android.app.ActivityThread.deliverResults(ActivityThread.java:4977)
E/AndroidRuntime(31316): at android.app.ActivityThread.handleSendResult(ActivityThread.java:5018)
E/AndroidRuntime(31316): at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
E/AndroidRuntime(31316): at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
E/AndroidRuntime(31316): at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
E/AndroidRuntime(31316): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2073)
E/AndroidRuntime(31316): at android.os.Handler.dispatchMessage(Handler.java:107)
E/AndroidRuntime(31316): at android.os.Looper.loop(Looper.java:225)
E/AndroidRuntime(31316): at android.app.ActivityThread.main(ActivityThread.java:7563)
E/AndroidRuntime(31316): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(31316): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
E/AndroidRuntime(31316): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:994)
E/AndroidRuntime(31316): Caused by: kotlin.UninitializedPropertyAccessException: lateinit property mresult has not been initialized
E/AndroidRuntime(31316): at com.media.storage.media_storage.MethodCallHandlerImpl.getMresult(MethodCallHandlerImpl.kt:60)
E/AndroidRuntime(31316): at com.media.storage.media_storage.MethodCallHandlerImpl.onActivityResult(MethodCallHandlerImpl.kt:291)
E/AndroidRuntime(31316): at io.flutter.embedding.engine.FlutterEngineConnectionRegistry$FlutterEngineActivityPluginBinding.onActivityResult(FlutterEngineConnectionRegistry.java:774)
E/AndroidRuntime(31316): at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.onActivityResult(FlutterEngineConnectionRegistry.java:422)
E/AndroidRuntime(31316): at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onActivityResult(FlutterActivityAndFragmentDelegate.java:964)
E/AndroidRuntime(31316): at io.flutter.embedding.android.FlutterActivity.onActivityResult(FlutterActivity.java:911)
E/AndroidRuntime(31316): at android.app.Activity.dispatchActivityResult(Activity.java:8249)
E/AndroidRuntime(31316): at android.app.ActivityThread.deliverResults(ActivityThread.java:4970)
E/AndroidRuntime(31316): ... 11 more
I/Process (31316): Sending signal. PID: 31316 SIG: 9
Lost connection to device.
Exited.
我尝试运行
flutter clean
和flutter pub get
,我尝试删除google-services.json
,firebase_options.json
,firebase.json
并再次运行flutterfire configure
,但这些并没有解决我的问题。
事实证明,我的应用程序因
media_storage
库而崩溃。现在,我必须找到一些替代方案来media_storage
。