🐙

【Flutter】Firebaseの初期化部分でのビルドエラー

2023/01/18に公開

ハマったこと

  • Flutterプロジェクトでfirebaseと連携し、Auth登録などをする際にアプリのビルドエラーが起きる

エラー文

[verbose-2:dart_vm_initializer.cc(41)] unhandled exception: [core/no-app] no firebase app '[default]' has been created - call firebase.initializeapp()

解決法

公式Docの解決手順を参照することもできます。

In the root of your application, run the configure command:
flutterfire configure
The configuration command will guide you through a number of processes:

  1. Selecting a Firebase project (based on the .firebaserc file or from the Firebase Console).
  2. Prompt what platforms (e.g. Android, iOS, macOS & web) you would like configuration for.
  3. Identify which Firebase applications for the chosen platforms should be used to extract configuration for. By default, the CLI will attempt to automatically match Firebase apps based on your current project configuration.
  4. Generate a firebase_options.dart file in your project.
    Once complete, you can now import the generated file and provide it to the initializeApp method:
    Then, provide the current platform options via the currentPlatform getter from the DefaultFirebaseOptions class:

要するに

  1. アプリのプロジェクトルートでflutterfire configureコマンド叩く(ターミナル)
  2. そのターミナルで今作ってるFirebase projectを選択
  3. 初期化設定を反映させたいプラットフォームを選択
    なんか聞かれたらYes選んだりパスワード打ち込んだりしつつ進んで、(すみませんあんま覚えてないです)
  4. ファイル内でirebase_options.dartを呼び出す(import 'firebase_options.dart';)。以下参照
  5. options:プロパティにoptions: DefaultFirebaseOptions.currentPlatform,を設定する

サンプルコード

main.dart
import 'firebase_options.dart';

void main() async{
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(//ここに
    options: DefaultFirebaseOptions.currentPlatform,
    //optionsプロパティを設定する
  );
  runApp(ProviderScope(child: const MyApp()));
}

最後に:調査中のこと

  • options: DefaultFirebaseOptions.currentPlatform,をなぜawait Firebase.initializeApp()の引数内に設定するのかはよくわかっていません。(少なくともこれが無くエラー吐かれていたので要るんですが。)

コメントありましたら教えて欲しいところです。🙇‍♂️自分でも調査中です。

GitHubで編集を提案

Discussion