Flutter TextField text not printing to variable?

7,963

This is how I managed to complete this with onChanged:...

new TextField(
                controller: _emailController,
                decoration:
                    new InputDecoration(labelText: "E M A I L   A D D R E S S"),
                onChanged: (str) {
                  setState(() {
                    str = _emailController.text;
                  });
                },
              ),

Future<Null> _loginButton() async {
    try {
      FirebaseAuth.instance.signOut();

      print("Login from Page");

      _email = _emailController.text.toString();
      _password = _passController.text.toString();
      _username = _nameController.text.toString();

      print(_email);
      print(_username);
      print(_password);

      await FirebaseAuth.instance
          .signInWithEmailAndPassword(email: _email, password: _password);

      var userid = FirebaseAuth.instance.currentUser.uid;

      if (FirebaseAuth.instance.currentUser.photoUrl == null) {
        fb.child("users/${userid}/image").onValue.listen((Event event) {
          print('Child added: ${event.snapshot.value}');
        });
      }

      print(FirebaseAuth.instance.currentUser);
    } catch (error) {
      print(error);
    }
  }
Share:
7,963
Charles Jr
Author by

Charles Jr

Jack of All Trades - Master of Few!! Started with Obj-C.then(Swift)!.now(Flutter/Dart)! Thank everyone for every answer, comment, edit, suggestion. Can't believe I've been able to answer a few myself :-)

Updated on December 03, 2022

Comments

  • Charles Jr
    Charles Jr over 1 year

    My textfield text for onChanged: I'm trying to change a variable I've declared in the same class and have it print in a voided function. Please explain how to print my textfield text.

    class LoginPageState extends State<LoginPage> {
      @override
    
      var _email;
      var _password;
      var _username;
    
      final TextEditingController controller = new TextEditingController();
    
    void _loginButton() {
    
        print("Login from Page");
        print(_password);
        print(_username);
        print(_email);
      }
    
    Widget build(BuildContext context) {
        return new Scaffold(
          backgroundColor: Colors.white70,
          appBar: new AppBar(
            backgroundColor: Colors.amber,
            title: new Text("Login / Signup"),
          ),
          body: new Container(
            child: new Center(
              child: new Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  new TextField(
                    controller: controller,
                    decoration:
                    new InputDecoration(labelText: "E M A I L   A D D R E S S"),
                    onChanged: (String newString){
                      setState((){
                        _email = controller.text;
    
                      });
                    },
                  ),
    
    • david.mihola
      david.mihola over 6 years
      You could call _loginButton() either before the return in build or as the last thing in the setState function. But I not sure I really understood your question...
  • Shady Aziza
    Shady Aziza over 6 years
    Why would you want to log out the user each time they attempt a log in ?
  • Shady Aziza
    Shady Aziza over 6 years
    Also onChanged will return the text being edited by the user, is not onSubmitted better be used here ?
  • Charles Jr
    Charles Jr over 6 years
    @aziza I'm calling log out to make sure the FirebaseUser.currentUser is always null before attempting to log another in. Also, I think your right about using onSubmitted... That way I should be able to use a .whenComplete call and auto route to my menu. Is this correct?
  • Shady Aziza
    Shady Aziza over 6 years
    I would suggest adding a sign out button somewhere that clears the user data when clicked and redirect them to the log in screen. And either you use onSubmitted to save the user data and navigates to your home, or use a Button as in my answer that does the same job when pressed.