TexteEditingController is always empty

Advertisements

I try to implement the email/password auth from Firebase but the emailController and passwordController send nothing tue my SubmitButton (or my TestButton)

class LoginScreen extends StatefulWidget {
  LoginScreen({super.key});

  @override
  State<LoginScreen> createState() => LoginScreenState();
}

class LoginScreenState extends State<LoginScreen> {
  final TextEditingController emailController = TextEditingController();
  final TextEditingController passwordController = TextEditingController();

  @override
  void dispose() {
    // Clean up the controller when the widget is disposed.
    emailController.dispose();
    passwordController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        padding: const EdgeInsets.all(30),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            LoginEmail(emailController: emailController),
            const SizedBox(height: 30.0),
            LoginPassword(passwordController: passwordController),
            const SizedBox(height: 30.0),
            SubmitButton(
              email: emailController.text.trim(),
              password: passwordController.text.trim(),
            ),
            TestButton(
              textTest: emailController.text.trim(),
            ),
            Flexible(
              child: LoginButton(
                text: 'Se connecter avec Google',
                icon: FontAwesomeIcons.google,
                color: Colors.blue,
                loginMethod: AuthService().googleLogin,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

LoginEmail

class LoginEmail extends StatelessWidget {
  LoginEmail({
    Key? key,
    required this.emailController,
  }) : super(key: key);

  final TextEditingController emailController;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: MediaQuery.of(context).size.width / 2,
      child: TextField(
        controller: emailController,
        decoration: const InputDecoration(hintText: 'Email'),
      ),
    );
  }
}

LoginPassword

class LoginPassword extends StatelessWidget {
  LoginPassword({
    Key? key,
    required this.passwordController,
  }) : super(key: key);

  final TextEditingController passwordController;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: MediaQuery.of(context).size.width / 2,
      child: TextField(
        controller: passwordController,
        obscureText: true,
        decoration: const InputDecoration(
          hintText: 'Password',
        ),
      ),
    );
  }
}

SubmitButton – $email and $password print nothing

class SubmitButton extends StatelessWidget {
  SubmitButton({
    Key? key,
    required this.email,
    required this.password,
  }) : super(key: key);

  final email;
  final password;
  final AuthService authService = AuthService();
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: () async {
        try {
          print('onPress email = $email et password = $password');
          await authService.signInWithEmailAndPassword(
            email: 'myEmail',
            password: 'myPw',
          );
          Navigator.of(context).pushReplacement(
              MaterialPageRoute(builder: (context) => HomeScreen()));
        } catch (e) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(
              content: Text(e.toString()),
            ),
          );
        }
      },
      child: const Text('Se connecter'),
    );
  }
}

TestButton – $textTest print nothing

class TestButton extends StatelessWidget {
  TestButton({
    Key? key,
    required this.textTest,
  }) : super(key: key);

  final textTest;

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: () async {
        try {
          print('onPress email = $textTest');
        } catch (e) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(
              content: Text(e.toString()),
            ),
          );
        }
      },
      child: const Text('Test'),
    );
  }
}

I was expecting to pass the value of emailController.text and passwordControlle.text to the SubmitButton.

>Solution :

When the widget build, it is getting string from controller. The initial text is empty for TextEditingController, and it is getting empty text on TestButton. You can pass controller instance itself, which will be passed by reference.

It will be like

class TestButton extends StatelessWidget {
  const TestButton({
    Key? key,
    required this.textTest,
  }) : super(key: key);

  final TextEditingController textTest;

And pass TextEditingController

 TestButton( textTest: emailController,),

Leave a ReplyCancel reply