Flutter集成Google Sign In点击按钮应用崩溃,请求帮助
Flutter Google登录点击崩溃的解决办法
问题情况
集成Google Sign In到Flutter应用后,点击登录按钮就崩溃,怀疑是iOS的info.plist里没配置CFBundleURLTypes,但不知道怎么加、加在哪。
用到的代码
GoogleSignInProvider类
class GoogleSignInProvider extends ChangeNotifier { GoogleSignInAccount? _user; GoogleSignInAccount get user => _user!; Future googleSignIn(context, {bool isLoading = true}) async { final googleSignIn = GoogleSignIn(); final googleAcount = await googleSignIn.signIn(); if (googleAcount != null) { final googleAuth = await googleAcount.authentication; if (googleAuth.accessToken != null && googleAuth.idToken != null) { try { await authInstance.signInWithCredential( GoogleAuthProvider.credential( idToken: googleAuth.idToken, accessToken: googleAuth.accessToken, ), ); Navigator.pushReplacement( context, MaterialPageRoute( builder: (context) => BtmNavBarScreen(), ), ); } on FirebaseException catch (error) { Methods.ErrorDailog(subtitle: '${error.message}', context: context); } catch (error) { Methods.ErrorDailog(subtitle: '$error', context: context); } finally {} } } } notifyListeners(); }
当前的info.plist
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>CADisableMinimumFrameDurationOnPhone</key> <true/> <key>CFBundleDevelopmentRegion</key> <string>$(DEVELOPMENT_LANGUAGE)</string> <key>CFBundleDisplayName</key> <string>Shop App</string> <key>CFBundleExecutable</key> <string>$(EXECUTABLE_NAME)</string> <key>CFBundleIdentifier</key> <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> <key>CFBundleInfoDictionaryVersion</key> <string>6.0</string> <key>CFBundleName</key> <string>shop_app</string> <key>CFBundlePackageType</key> <string>APPL</string> <key>CFBundleShortVersionString</key> <string>$(FLUTTER_BUILD_NAME)</string> <key>CFBundleSignature</key> <string>????</string> <key>CFBundleVersion</key> <string>$(FLUTTER_BUILD_NUMBER)</string> <key>LSRequiresIPhoneOS</key> <true/> <key>UIApplicationSupportsIndirectInputEvents</key> <true/> <key>UILaunchStoryboardName</key> <string>LaunchScreen</string> <key>UIMainStoryboardFile</key> <string>Main</string> <key>UISupportedInterfaceOrientations</key> <array> <string>UIInterfaceOrientationPortrait</string> <string>UIInterfaceOrientationLandscapeLeft</string> <string>UIInterfaceOrientationLandscapeRight</string> </array> <key>UISupportedInterfaceOrientations~ipad</key> <array> <string>UIInterfaceOrientationPortrait</string> <string>UIInterfaceOrientationPortraitUpsideDown</string> <string>UIInterfaceOrientationLandscapeLeft</string> <string>UIInterfaceOrientationLandscapeRight</string> </array> <key>UIViewControllerBasedStatusBarAppearance</key> <false/> </dict> </plist>
修复步骤
1. 给info.plist加CFBundleURLTypes
CFBundleURLTypes要放在plist里的<dict>标签内部,随便找个空位置插就行,比如放在最后一个key-value对之后、</dict>之前。
先去Firebase控制台找到你iOS应用的REVERSED_CLIENT_ID(格式是com.googleusercontent.apps.xxxxxx-xxxxxx),替换下面代码里的YOUR_GOOGLE_REVERSED_CLIENT_ID:
<key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleTypeRole</key> <string>Editor</string> <key>CFBundleURLSchemes</key> <array> <string>YOUR_GOOGLE_REVERSED_CLIENT_ID</string> </array> </dict> </array>
改完后的完整info.plist示例:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>CADisableMinimumFrameDurationOnPhone</key> <true/> <key>CFBundleDevelopmentRegion</key> <string>$(DEVELOPMENT_LANGUAGE)</string> <key>CFBundleDisplayName</key> <string>Shop App</string> <key>CFBundleExecutable</key> <string>$(EXECUTABLE_NAME)</string> <key>CFBundleIdentifier</key> <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> <key>CFBundleInfoDictionaryVersion</key> <string>6.0</string> <key>CFBundleName</key> <string>shop_app</string> <key>CFBundlePackageType</key> <string>APPL</string> <key>CFBundleShortVersionString</key> <string>$(FLUTTER_BUILD_NAME)</string> <key>CFBundleSignature</key> <string>????</string> <key>CFBundleVersion</key> <string>$(FLUTTER_BUILD_NUMBER)</string> <key>LSRequiresIPhoneOS</key> <true/> <key>UIApplicationSupportsIndirectInputEvents</key> <true/> <key>UILaunchStoryboardName</key> <string>LaunchScreen</string> <key>UIMainStoryboardFile</key> <string>Main</string> <key>UISupportedInterfaceOrientations</key> <array> <string>UIInterfaceOrientationPortrait</string> <string>UIInterfaceOrientationLandscapeLeft</string> <string>UIInterfaceOrientationLandscapeRight</string> </array> <key>UISupportedInterfaceOrientations~ipad</key> <array> <string>UIInterfaceOrientationPortrait</string> <string>UIInterfaceOrientationPortraitUpsideDown</string> <string>UIInterfaceOrientationLandscapeLeft</string> <string>UIInterfaceOrientationLandscapeRight</string> </array> <key>UIViewControllerBasedStatusBarAppearance</key> <false/> <!-- Google登录URL配置 --> <key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleTypeRole</key> <string>Editor</string> <key>CFBundleURLSchemes</key> <array> <string>com.googleusercontent.apps.xxxxxx-xxxxxx</string> </array> </dict> </array> </dict> </plist>
2. 额外检查项
- 确认Firebase控制台的REVERSED_CLIENT_ID和info.plist里的完全一致,多一个少一个字符都不行。
- 清理Xcode缓存(Product -> Clean Build Folder),重启IDE后重新编译运行。
- 检查
authInstance是不是正确初始化的FirebaseAuth实例(比如FirebaseAuth.instance)。 - 确保登录按钮的点击事件确实调用了
googleSignIn方法,并且上下文参数没传错。
内容的提问来源于stack exchange,提问作者Yousif 304
相关产品推荐
相关产品推荐

