Ali
Ali

Reputation: 31

Flutter-Firebase how to push Future<FirebaseUser> to another class

I'm trying to create Authentication forms. The user can register with e-mail and password. After eMailandPassword registration is completed successfully, the user is redirected to Verification page. I need user's info so that I can register his phone number to the firebase. Here is my createUser function;

    void createUser() async {
        dynamic result = await _auth.createNewUser(
            _nameController.text, _emailContoller.text, _passwordController.text);
        if (result == null) {
          print('Email is not valid');
        } else {
          print(result.toString());
          _nameController.clear();
          _passwordController.clear();
          _emailContoller.clear();
    
          Future<FirebaseUser> user = _auth.getCurrentUser();
**/////HOW TO PASS user to the PhoneVerification class. ??**

          Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => PhoneVerification(user:user)),
          );
        }
      }

If I pass as Future user, I cannot access its value in PhoneVerification class.

class PhoneVerification extends StatefulWidget {
  final Future<FirebaseUser> user;
  const PhoneVerification({Key? key, required this.user}) : super(key: key);

  @override
  _PhoneVerificationState createState() => _PhoneVerificationState();
}

Any suggestion? Thank you in advance

Upvotes: 0

Views: 51

Answers (2)

Huthaifa Muayyad
Huthaifa Muayyad

Reputation: 12383

  void createUser() async {
        dynamic result = await _auth.createNewUser(
            _nameController.text, _emailContoller.text, _passwordController.text);
        if (result == null) {
          print('Email is not valid');
        } else {
          print(result.toString());
          _nameController.clear();
          _passwordController.clear();
          _emailContoller.clear();
    
          FirebaseUser user = await _auth.getCurrentUser();//<=== await here


          Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => PhoneVerification(user:user)),
          );
        }
      }

But as Frank mentioned, are you sure you want to be passing a FirebaseUser object to your other class? The documentation explains well how to manage phone auth.

Upvotes: 1

mike-gallego
mike-gallego

Reputation: 724

void createUser() async {
    dynamic result = await _auth.createNewUser(
        _nameController.text, _emailContoller.text, _passwordController.text);
    if (result == null) {
      print('Email is not valid');
    } else {
      print(result.toString());
      _nameController.clear();
      _passwordController.clear();
      _emailContoller.clear();

      FirebaseUser user = await _auth.getCurrentUser();

      Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => PhoneVerification(user:user)),
      );
    }
  }

class PhoneVerification extends StatefulWidget {
 final FirebaseUser user;
 const PhoneVerification({Key? key, required this.user}) : super(key: key);

 @override
 _PhoneVerificationState createState() => _PhoneVerificationState();
}

Upvotes: 1

Related Questions