skip to Main Content

enter image description here

I am able to create new user but having trouble registering the credentials to Firestore.

Can someone pls tell me what am I doing wrong??

here’s the code sample where I am registering user.

added necesary dependencies and code

2

Answers


  1. Did you listen the event when authStateChanges?

    Try to add this code to onInit

    FirebaseAuth.instance
      .authStateChanges()
      .listen((User? user) {
        if (user == null) {
           print('User is currently signed out!');
        } else {
           print('User is signed in!');
        }
       });
    
    Login or Signup to reply.
  2. If you are having trouble registering users and storing their credentials in Firestore, you need to include the registration logic. Here’s a general outline of how you can register a new user and store their information in Firestore using the Firebase services:

    // Function to register a new user
    Future<void> registerUser(String email, String password) async {
      try {
        // Create user with email and password
        await FirebaseAuth.instance.createUserWithEmailAndPassword(
          email: email,
          password: password,
        );
    
    // Access the current user after registration
    User? user = FirebaseAuth.instance.currentUser;
    
    // Store user data in Firestore
    await FirebaseFirestore.instance.collection('users').doc(user!.uid).set({
      'email': email,
      // Add additional user data as needed
    });
    
    print('User registered successfully!');
    
    
    } catch (e) {
        print('Error registering user: $e');
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search