1
0

More than 1 year has passed since last update.

flutter x firebaseのinitializeで困ったのでメモ

Last updated at Posted at 2021-12-15

ここの記事に沿って、チュートリアル進めていたところ、Firebase周りでつまづいたので備忘録です。

準備したこと

  • firebaseコンソールからproject作成
  • pubspec.yamlに下記記載して、pub get
  • firebaseコンソールからメールアドレスでのauthを有効化(メールでauthを使う場合)
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: ^1.10.5
  firebase_auth: ^3.0.3

Firebase authを使おうとしたら、、、initializeが必要とのこと

ElevatedButton(
                  onPressed: () async {
                    try {

                      final FirebaseAuth auth = FirebaseAuth.instance;


                      developer.log("email=$_email, password=$_password");
                      final UserCredential result =
                      await auth.createUserWithEmailAndPassword(
                          email: _email, password: _password);
                      final user = result.user;
                      setState(() {
                        _infoText = "登録OK: ${user?.email}";
                      });
                    } catch (e) {
                      developer.log(e.toString());
                      setState(() {
                        _infoText = "登録NG: ${e.toString()}";
                      });
                    }

                  },
                  child: Text("登録"))

以下のエラーが出てくる。どうやら、initializationが必要らしい。

Firebase has not been corre…ase.flutter.dev/docs/overview#initialization

Firebase をinitialize するもエラー

こちらの記事に沿って、initializeを試すことに。


void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

次は別のエラーが出現!!!!

MissingPluginException(No implementation found for method Firebase#initializeCore on channel plugins.flutter.io/firebase_core)

FlutterFireのドキュメントに沿って解決

ドキュメントを読み直す

CLIツールがインストールされていないことに気付いたので、公式サイトよりインストール

$ curl -sL https://firebase.tools | bash

FIrebaseにログイン

$ firebase login

cliをactivate して、flutterfireで設定を行う。自動で、指定したアプリをfirebase project上に登録してくれる。(android, ios, web app など)

# Install the CLI if not already done so
dart pub global activate flutterfire_cli

# Run the `configure` command, select a Firebase project and platforms
flutterfire configure

改めて、main.dartにて、initialization

import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  runApp(MyApp());
}

=>Androidで、すでにインストールされていたアプリをアンインストールして、インストールし直し
=>無事、Firebase authが使えました〜〜〜!!

1
0
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
1
0