0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

DartError: Assertion failed: ...firebase_core_web.dart:283:11 options != nullを解決する

Posted at

何をしたかったか

FlutterとFirebaseを同期させようとしたかったのですが、Firebaseの初期化がうまく行きませんでした。

コード

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

エラー文原文

DartError: Assertion failed: file:///Users/user-name/.pub-cache/hosted/pub.dev/firebase_core_web-2.18.1/lib/src/firebase_core_web.dart:283:11
options != null
"FirebaseOptions cannot be null when creating the default app."
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 296:3  throw_
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 29:3   assertFailed
packages/firebase_core_web/src/firebase_core_web.dart 283:18                 initializeApp
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 45:50           <fn>
dart-sdk/lib/async/zone.dart 1661:54                                         runUnary
dart-sdk/lib/async/future_impl.dart 163:18                                   handleValue
dart-sdk/lib/async/future_impl.dart 847:44                                   handleValueCallback
dart-sdk/lib/async/future_impl.dart 876:13                                   _propagateToListeners
dart-sdk/lib/async/future_impl.dart 652:5                                    [_completeWithValue]
dart-sdk/lib/async/future.dart 532:22                                        <fn>
dart-sdk/lib/async/zone.dart 1661:54                                         runUnary
dart-sdk/lib/async/future_impl.dart 163:18                                   handleValue
dart-sdk/lib/async/future_impl.dart 847:44                                   handleValueCallback
dart-sdk/lib/async/future_impl.dart 876:13                                   _propagateToListeners
dart-sdk/lib/async/future_impl.dart 652:5                                    [_completeWithValue]
dart-sdk/lib/async/future_impl.dart 722:7                                    callback
dart-sdk/lib/async/schedule_microtask.dart 40:11                             _microtaskLoop
dart-sdk/lib/async/schedule_microtask.dart 49:5                              _startMicrotaskLoop
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 181:7           <fn>

ポイントは、

"FirebaseOptions cannot be null when creating the default app."

options != null

の二箇所だと判断しました。

原因

Firebaseの初期化時に、オプションが指定されていませんでした。

改善案

初期化時に、任意のオプションを指定します。今回は、公式サイトの手順にありました"DefaultFirebaseOptions.currentPlatform"を使用しました。

改善コード

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
  runApp(const MyApp());
}

これで、初期化に関する問題は解決しました。次の記述でも行えるそうです。

  await Firebase.initializeApp(
  options: FirebaseOptions(
      apiKey: apiKey, 
      appId: appId, 
      messagingSenderId: messagingSenderId, 
      projectId: projectId
    ))

参考文献

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?