Flutter × Firebase で Google ログイン時に GoogleSignInException: serverClientId must be provided エラーが出る場合の解決方法
原因
Android 環境で Google ログインを行う際、serverClientId が設定されていないことが原因です。
Flutter アプリはネイティブアプリであっても Google Cloud Console の「Web クライアント」ID を serverClientId に渡す必要があります。
解決方法
-
Google Cloud Console を開く
https://console.cloud.google.com/apis/credentials -
左上のプロジェクト選択から、エラーが発生している Firebase プロジェクト を選択
-
OAuth 2.0 クライアント ID の一覧から Web client を開く
※Flutter アプリはスマホアプリですが、ここでは Web client が正しいです
-
画面右の Additional information から「クライアント ID」をコピー
-
firebase_options.dartのandroidのFirebaseOptionにあるandroidClientIdに代入FirebaseOptions( androidClientId: '<コピーしたクライアントID>', ... )
GoogleSignIn.instance.initialize() を呼び出している箇所を以下のように修正
await GoogleSignIn.instance.initialize(
serverClientId: '<FirebaseOptionのandroidClientId>'
);
参考
https://github.com/flutter/flutter/issues/172073#issuecomment-3067310666