はじめに
FirebaseプロジェクトをFlutterアプリと連携させるときは、iOSとAndroidで別々の設定を行なっていた記憶なんですが
知らぬ間に2つ同時にできるようになっていました!
環境
Firebase CLIはすでに設定済み
https://firebase.google.com/docs/cli?authuser=0&hl=ja#install_the_firebase_cli
MacOS Monterey
まずはFirebaseにログイン
$ firebase login
Already logged in as test@gmail.com
Firebase CLIでログインできていることを確認
flutterfire_cliを入れる
$ dart pub global activate flutterfire_cli
省略
Building package executables... (3.8s)
Built flutterfire_cli:flutterfire.
Installed executable flutterfire.
Warning: Pub installs executables into $HOME/.pub-cache/bin, which is not on your path.
You can fix that by adding this to your shell's config file (.bashrc, .bash_profile, etc.):
export PATH="$PATH":"$HOME/.pub-cache/bin"
Activated flutterfire_cli 0.2.7.
新たにパスを通す必要があるようなので.zshrc
ファイルを編集し、👇を追加(シェルにbashを使ってる人は.bashrc
)
export PATH="$PATH":"$HOME/.pub-cache/bin"
flutterfireの設定をする
ターミナルを再起動し、Flutter プロジェクト ディレクトリのルートで次のコマンドを実行
$ flutterfire configure --project=プロジェクトID
? Which platforms should your configuration support (use arrow keys & space to select)? ›
✔ android
✔ ios
macos
✔ web
どのプラットフォームで使いたいかを聞かれるのでスペースキーでチェックを外しながら選びます。
The files android/build.gradle & android/app/build.gradle will be updated to apply Firebase configuration and gradle build plugins. Do you want to continue? (y/n) › yes
Yesのyを選んで各build
ファイルにも適応させます。
完了後、firebase_options.dart
が生成されます!
firebase_options.dart
を覗いてみるとまぁびっくり!
今までコピペして作っていたFirebaseOptionsが既にできてある
これはありがたい。
更にGoogleService-Info.plist
やgoogle-services.json
といった設定ファイルも自動で作られています!!
必要パッケージを追加
パッケージが存在しないためfirebase_options.dart
でエラーが発生しているかと。
最低でもfirebase_core
が必要となるのでこちらをインストール
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
firebase_core: ^2.4.0
main.dart
でFirebaseを初期化
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'firebase_options.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
runApp(const MyApp());
}
これでFirebaseを使う準備は整いました!!!
最後に
どうやらFlutter 2.8のアップデートと共に、Flutter×Firebaseも大きくアップデートされたようでした!
👇に詳しく載っています