flutterfire CLI toolを使ってみる
flutterfire
CLIツールでそれぞれのプラットフォームの初期化オプションデータ構造を集められるようになりました。
このツールで新しいFirebaseプロジェクトを作成することが可能です。
これが気になったので、使ってみる。
事前準備
flutterfire
dart pub global activate flutterfire_cli
インストールすると、 export PATH="$PATH":"$HOME/.pub-cache/bin"
を追加するように書かれていたので、 .zshrc
に追加。
firebase CLI (npm経由)
ERROR: The FlutterFire CLI currently requires the official Firebase CLI to also be installed, see https://firebase.google.com/docs/cli#install_the_firebase_cli for how to install it.
と言われたので、まずはnpmのバージョンアップ。
asdf install nodejs latest
asdf global nodejs latest
今回は、 17.2.0 がインストールされた。
npm install -g firebase-tools
firebase CLI でのログイン
別アカウントでログインした形跡があったので、まずは firebase logout
。
その後、 firebase login
を実行するとブラウザが開くので、そこでログインする。
Flutterプロジェクトの作成
flutterfire configure
作成済みプロジェクトと、 <create a new project>
が表示されるので、選択。
"Enter a project id for your new Firebase project (e.g. my-cool-project)" と聞かれるので、任意の名前を入力。
"Which platforms should your configuration support (use arrow keys & space to select)?" と聞かれる。選択肢は "android", "ios", "macos", "web"。
"Which ios bundle id do you want to use for this configuration, e.g. 'com.example.app'?" と聞かれるので、bundle idを入力。(iosを選択したからだと思われる)
結果として、 lib/firebase_options.dart
が出力される。
("android", "ios", "web" を選択した場合)
// File generated by FlutterFire CLI.
// ignore_for_file: lines_longer_than_80_chars
import 'package:firebase_core/firebase_core.dart' show FirebaseOptions;
import 'package:flutter/foundation.dart'
show defaultTargetPlatform, kIsWeb, TargetPlatform;
/// Default [FirebaseOptions] for use with your Firebase apps.
///
/// Example:
/// ```dart
/// import 'firebase_options.dart';
/// // ...
/// await Firebase.initializeApp(
/// options: DefaultFirebaseOptions.currentPlatform,
/// );
/// ```
class DefaultFirebaseOptions {
static FirebaseOptions get currentPlatform {
if (kIsWeb) {
return web;
}
// ignore: missing_enum_constant_in_switch
switch (defaultTargetPlatform) {
case TargetPlatform.android:
return android;
case TargetPlatform.iOS:
return ios;
case TargetPlatform.macOS:
throw UnsupportedError(
'DefaultFirebaseOptions have not been configured for macos - '
'you can reconfigure this by running the FlutterFire CLI again.',
);
}
throw UnsupportedError(
'DefaultFirebaseOptions are not supported for this platform.',
);
}
static const FirebaseOptions web = FirebaseOptions(
apiKey: 'AAA',
appId: '1:999:web:111',
messagingSenderId: '999',
projectId: 'prj-aaa',
authDomain: 'prj-aaa.firebaseapp.com',
storageBucket: 'prj-aaa.appspot.com',
);
static const FirebaseOptions android = FirebaseOptions(
apiKey: 'BBB',
appId: '1:999:android:222',
messagingSenderId: '999',
projectId: 'prj-aaa',
storageBucket: 'prj-aaa.appspot.com',
);
static const FirebaseOptions ios = FirebaseOptions(
apiKey: 'CCC',
appId: '1:999:ios:333',
messagingSenderId: '999',
projectId: 'prj-aaa',
storageBucket: 'prj-aaa.appspot.com',
iosClientId: '999-zzzz.apps.googleusercontent.com',
iosBundleId: 'com.example.prjAaa',
);
}
firebase_core
が解決できなかったので、 flutter pub add firebase_core
を実行。
iOSのビルドに失敗したので、CocoaPodsの入れ直し。
asdf install ruby latest
asdf global ruby latest
gem install cocoapods
pod repo update
再ビルドしたら、起動した。
dev向けと本番向けなど、bundle idやapplication idを分けておく場合が多い。
flutterfire configure
のオプションや、 dart-define による起動切り替えが参考になった記事。