0

so I was trying to make a flutter app with firebase , it works perfectly fine but when I run the app , I get this error :

════════ Exception caught by widgets library ═══════════════════════════════════
The following FirebaseException was thrown building _BodyBuilder:
[core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()

    The relevant error-causing widget was
    Scaffold
    package:app_with_firebase/main.dart:14
    When the exception was thrown, this was the stack
    #0      MethodChannelFirebase.app
    package:firebase_core_platform_interface/…/method_channel/method_channel_firebase.dart:122
    #1      Firebase.app
    package:firebase_core/src/firebase.dart:54
    #2      FirebaseAuth.instance
    package:firebase_auth/src/firebase_auth.dart:37
    #3      new AuthService
    package:app_with_firebase/Services/auth.dart:4
    #4      new _LoginPageState
    package:app_with_firebase/main.dart:29
    ...
    ════════════════════════════════════════════════════════════════════════════════

But the thing is that I have that line inside my code :

//import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'Services/auth.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  Firebase.initializeApp();
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: LoginPage(),
      ),
    ),
  );
}

How can I get rid of this ? The app runs fine but it is very distracting , any help is appreciated , thanks.

Keemo Kimo
  • 106
  • 1
  • 13
  • Could you add await Firebase.initializeApp(); instead, I suspect that the build is call before Firebase calling finish – Einzeln Jul 23 '21 at 01:24
  • @Einzeln , Thank you! I added await infront of Firebase.initializeApp() , and now it works like a charm ! – Keemo Kimo Jul 23 '21 at 01:33

1 Answers1

1

add await keyword in front of Firebase.initializeApp();
Since it is time-consuming function you should wait some time in that statement. If you miss that await keyword it should return null value.

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(
  MaterialApp(
    debugShowCheckedModeBanner: false,
    home: Scaffold(
      body: LoginPage(),
    ),
  ),
);
}