-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
  • 15,750
  • 5
  • 43
  • 62
  • when I call the function register and the field 'app' is required, when I try to put the variable ```app```, ```FirebaseApp app = await Firebase.initializeApp( name: 'secondary', options: Firebase.app().options);```, it does not find it. How do I call the variable app from the main function – Mohammad Abd-Elmoniem Aug 22 '21 at 16:03
  • I have an onpressed function for the register, and it has an onpressed. I cant put register(app, context), it cant find the app variable i put inside of the main function – Mohammad Abd-Elmoniem Aug 22 '21 at 16:04
  • Are you intentionally creating a `second` app? Is there any requirement for that? – tmarwen Aug 22 '21 at 18:48
  • No, I just want the account to get created without loggin into it – Mohammad Abd-Elmoniem Aug 22 '21 at 18:49
  • I have no idea what app: secondary is – Mohammad Abd-Elmoniem Aug 22 '21 at 18:50
  • Change whatever code, as long as it can create an account with logging into it, thats all – Mohammad Abd-Elmoniem Aug 22 '21 at 18:50
  • the updates are the same as my code – Mohammad Abd-Elmoniem Aug 22 '21 at 19:17
  • what do you mean by '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:' – Mohammad Abd-Elmoniem Aug 22 '21 at 19:21
  • I am new to flutter, so I dont understand some terms – Mohammad Abd-Elmoniem Aug 22 '21 at 19:21
  • 1
    Simply said, if you have well setup firebase for Flutter (the code you are not showing here), you don't need to pass the `app` instance as argument and you don't have to call `FirebaseAuth.instanceFor` specifying the app but you can simply use the `FirebaseAuth.instance` instead. Is that clearer? – tmarwen Aug 22 '21 at 19:24
  • so do I put this await Firebase.initializeApp(); in the top of the future function – Mohammad Abd-Elmoniem Aug 22 '21 at 19:28
  • You should call it on your mai application function. If you update the post with your Flutter application main entry I would be able to help. – tmarwen Aug 22 '21 at 23:49