0

I have a problem. I'm working on an application with Flutter 3 and I have to know if my TextField is returning the user entries. I have on one side this function to test signIn with the predefined user. Prints are here to verify the users entries. The signIn is working well but the print are empty. This one is contained in a stateless class called "LoginPage".

void loginToFirebase() {
    print(FormSection().emailField.text.trim());
    print(FormSection().passwordField.text.trim());
    try {
      auth
          .signInWithEmailAndPassword(
              email: 'xxxx@xxxx.com',
              password: 'motdepasse')
          .then((value) {
        print(value.toString());
      });
    } catch (e) {
      print(e.toString());
    }
  }

On second side I have my two TextField (the email and the password have the same construction, only the controller variable is changing). This one is contained in a stateless class called "FormSection".

Container(
                margin: EdgeInsets.only(left: 10),
                height: 60,
                width: 230,
                child: Center(
                  child: TextField(
                    controller: passwordField,
                    textAlign: TextAlign.center,
                    obscureText: true,
                    style: GoogleFonts.comfortaa(
                        fontSize: 20,
                        color: Colors.white,
                        fontWeight: FontWeight.bold),
                    decoration: InputDecoration(
                      hintText: 'Mot de passe',
                      hintStyle: GoogleFonts.comfortaa(
                        color: Colors.white.withOpacity(0.5)),
                      border: InputBorder.none),
                )),
              )

When I press the ElevatedButton (my connection button), I can login all it's ok, but the Controller.text is empty.

Someone have a solution ? Thanks for the time taken to read my problem and to answer.

I'm trying to call the user text input to verify the account to compare to my database.

Lucas
  • 26
  • 3
  • you are creating new instance every time with `FormSection()` can you include your class a minimal full widget – Md. Yeasin Sheikh Nov 04 '22 at 16:07
  • the connection is working well, my problem is just about the TextEditingController.text i don't understand what are you saying about "include your class a minimal full widget" – Lucas Nov 04 '22 at 16:20
  • I think he's trying to say that in the first snippet when you use `FormSection()` you are creating a new instance of FormSection, therefore `emailField` and `PasswordField` are probably null or empty (unless FormSection is a Singleton or those) I would recommend using some state management solution, or instead using a `StatefulWidget` to handle both forms states/values, and then providing those values to the TextFields – Fabián Bardecio Nov 04 '22 at 17:30
  • Sure ! Thanks for the idea, I tried to use a StatefulWidget with an Listener on my both controller. And it's work ! Thanks for the idea ! – Lucas Nov 05 '22 at 13:44

1 Answers1

0

Your two controllers are emailField and passwordField. And if you are logging in correctly, they aren't empty of course. I don't see any third controller called Controller (which you wouldn't be able to call it that, I think), nor can you call it TextEditingController, since that is the Class name.

Your named controllers are emailField and passwordField: you get the text from them by calling emailField.text and passwordField.text.

Please elaborate with more of your code if I'm missing something.

jbryanh
  • 1,193
  • 7
  • 17
  • I'm calling two controllers emailField and passwordField, the both are connected to the respectives TextField. In my class FormSection i'm initializing the both controller and i'm connection them to the TextField. When I'm pressing the button I'm printing the controller.text.trim() to verify the content and they are empty. I don't understand why. – Lucas Nov 05 '22 at 13:13
  • because you are calling controller.text.trim() and you should be calling emailField .text.trim() and passwordField.text.trim() – jbryanh Nov 07 '22 at 15:38