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?

【Flutter】WidgetsFlutterBinding.ensureInitialized()ってなんだっけ

Last updated at Posted at 2025-07-06

WidgetsFlutterBinding.ensureInitialized()とは

結論:
WidgetsFlutterBinding.ensureInitialized(); は、
Flutterエンジンを確実に初期化するためのコマンド

以下のような 非同期処理を main() 関数で使う前に必要:

  • Firebase.initializeApp()(Firebaseの初期化)
  • SharedPreferences.getInstance()(ローカルデータ取得)
  • rootBundle.load()(アセット読み込み)

これらは、Flutterのバインディング(描画やサービスアクセスの準備)が
まだ終わっていない状態では使えず、エラーになることがある。

使用例(Firebase + Riverpod構成)

void main() async {
  WidgetsFlutterBinding.ensureInitialized(); // Flutterの初期化

  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );

  runApp(const ProviderScope(child: MyApp())); // Riverpodのルートスコープ
}

まとめ

  • main()async かつ await を使うときは必ず書く
  • 書かないと、初期化されていない状態でエラーになることがある
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?