skip to Main Content
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_firebase_test/firebase_options.dart';

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

when i remove two line of code it work fine.

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

Ui should be loaded as it is before.

2

Answers


  1. you need to add WidgetsFlutterBinding.ensureInitialized();.

        void main() async {
          WidgetsFlutterBinding.ensureInitialized();
          await Firebase.initializeApp(
            options: DefaultFirebaseOptions.currentPlatform,
          );
          runApp(MyApp());
        }
    
    Login or Signup to reply.
  2. When you write the main method as asynchronous, and you run a Flutter app, the asynchronous method waits for the Firebase connection. As a result, your UI will be paused. To avoid this problem, you can write it like this:

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

    So try to avoid main method as as async and also not write await method in main method

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search