1

Scenario: I have three screen of an app that I launch based on condition. One screen is buttons with other two screen options.

It works fine on a screen where I am initialising the firebase and doing fetching and all the stuff fine. void startFirebase() async { await Firebase.initializeApp(); } . However on a second screen I am doing initialisation same way but I am encountered with this error: No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp().

Question: How to check if it is initialised (to check if initialised on first screen and wont reinitialise on second one) and - what will happen if I initialise Firebase on both the screens or if initialised twice?

update based on first provided answer:

I am not sure but calling initilise twice does not show any error. Heres how I'm trying twice:

@override
 void initState() {
    startFirebase();
    try{
      startFirebase();
    }catch(e){
      print(e.toString());
    }
    super.initState();
  }

//another way: 

@override
  void initState() {
    startFirebase();
    startFirebase();

/*    try{
      startFirebase();
    }catch(e){
      print(e.toString());
    }*/
    super.initState();
  }

No error on run tab and app works fine.

Rifat
  • 1,700
  • 3
  • 20
  • 51

1 Answers1

0

Firebase init will fail with a different message if you attempt to do it a second time.

FirebaseApp name [DEFAULT] already exists

You can check if it's already initialized as described in this other question.

Unless you have specific needs, you should instead consider instead initializing Firebase just once globally for your main app object when it first launches, and don't worry about it again after that.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • I checked that answer previously and I have updated the question with trial. Please check it. Thank you for your suggestion. – Rifat Dec 27 '20 at 05:38