-1

I tried to follow the solution here, Flutter: Firebase authentication create user without logging In,

Future<void> register(BuildContext context) async {
    FirebaseApp app = await Firebase.initializeApp(
        name: 'secondary', options: Firebase.app().options);
    try {
      UserCredential userCredential = await FirebaseAuth.instanceFor(app: app)
          .createUserWithEmailAndPassword(
              email: email.text, password: password.text);
      Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => createSetupPage()),
      );
    } on FirebaseAuthException catch (e) {
      if (e.code == 'weak-password') {
        setState(() {
          ScaffoldMessenger.of(context).showSnackBar(SnackBar(
            content: Text('Weak Password'),
          ));
        });
      } else if (e.code == 'email-already-in-use') {
        ScaffoldMessenger.of(context).showSnackBar(SnackBar(
          content: Text('Email Already In Use'),
        ));
      }
    } catch (e) {
      print(e);
    }
  }

When I create an account, it works once then gives me this error when I try using this code to make another account with the same name, it gives me this error in the console

E/flutter ( 1191): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: [core/duplicate-app] A Firebase App named "secondary" already exists

How do I fix this so that I can keep making multiple accounts without getting logged out from one account? It seems that I need a new name every in the Firebase.initialize app part.

1 Answers1

1

Your Firebase app instance should be initialized only once per-application launch.

You should then move the initialization block:

FirebaseApp app = await Firebase.initializeApp(
    name: 'secondary', options: Firebase.app().options);

outside of method calls and have initialized at startup:

void main() async {
    FirebaseApp app = await Firebase.initializeApp(
        name: 'secondary', options: Firebase.app().options);
    //...
}

then you can have the FirebaseApp instance passed as an argument to callees whenever needed:

Future<void> register(FirebaseApp app, BuildContext context) async {
    try {
      UserCredential userCredential = await FirebaseAuth.instanceFor(app: app)
          .createUserWithEmailAndPassword(
              email: email.text, password: password.text);
      Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => createSetupPage()),
      );
    } on FirebaseAuthException catch (e) {
      if (e.code == 'weak-password') {
        setState(() {
          ScaffoldMessenger.of(context).showSnackBar(SnackBar(
            content: Text('Weak Password'),
          ));
        });
      } else if (e.code == 'email-already-in-use') {
        ScaffoldMessenger.of(context).showSnackBar(SnackBar(
          content: Text('Email Already In Use'),
        ));
      }
    } catch (e) {
      print(e);
    }
}

That being said, you can totally omit passing the FirabseApp instance around as the firebase_auth library will keep hold of the default app instance upon bootstrapping.

Having called await Firebase.initializeApp(); upon your application starting step, you can then simply access the FirebaseAuth.instance referencing the default app:

Future<void> register(BuildContext context) async {
    try {
      UserCredential userCredential = await FirebaseAuth.instance.createUserWithEmailAndPassword(
              email: email.text, password: password.text);
      Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => createSetupPage()),
      );
    } on FirebaseAuthException catch (e) {
      if (e.code == 'weak-password') {
        setState(() {
          ScaffoldMessenger.of(context).showSnackBar(SnackBar(
            content: Text('Weak Password'),
          ));
        });
      } else if (e.code == 'email-already-in-use') {
        ScaffoldMessenger.of(context).showSnackBar(SnackBar(
          content: Text('Email Already In Use'),
        ));
      }
    } catch (e) {
      print(e);
    }
}
tmarwen
  • 14,219
  • 4
  • 40
  • 59