5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

いつの間にかFlutter × Firebaseの連携が更に簡単になっていた

Posted at

はじめに

FirebaseプロジェクトをFlutterアプリと連携させるときは、iOSとAndroidで別々の設定を行なっていた記憶なんですが:neutral_face:
知らぬ間に2つ同時にできるようになっていました!

Screen Shot 2022-12-10 at 20.35.02.png

環境

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でログインできていることを確認

確認できたらガイドに沿って進めていきます。
Screen Shot 2022-12-10 at 20.43.47.png

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

.zshrc
  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が既にできてある:heart_eyes:
これはありがたい。

更にGoogleService-Info.plistgoogle-services.jsonといった設定ファイルも自動で作られています!!

必要パッケージを追加

パッケージが存在しないためfirebase_options.dartでエラーが発生しているかと。
最低でもfirebase_coreが必要となるのでこちらをインストール

pubspec.yaml
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を初期化

main.dart
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も大きくアップデートされたようでした!
👇に詳しく載っています

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?