0

I'm getting this error while running my application

LateInitializationError: Field '_email@598051279' has already been initialized.

How can i fix this problem?

this my code divided in two parts:

  
class _LoginViewState extends State<LoginView> {
  late final TextEditingController _email;
  late final TextEditingController _password;

  @override
  void dispose() {
    _email = TextEditingController();
    _password = TextEditingController();
    super.dispose();
  }

  @override
  void initState() {
    _email = TextEditingController();
    _password = TextEditingController();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Login')),
      body: Column(
        children: [
          TextField(
            controller: _email,
          ),
          TextField(
            controller: _password,
          ),

this is the second part

          Center(
            child: TextButton(
                onPressed: () async {
                  final email = _email.text;
                  final password = _password.text;
                  try {
                    final userCredintials =
                        await FirebaseAuth.instance.signInWithEmailAndPassword(
                      email: email,
                      password: password,
                    );
                    Navigator.of(context).pushNamedAndRemoveUntil(
                      notesRoute,
                      (route) => false,
                    );
                  } 
        ],
      ),
    );
  }
}

here is depug-console

LateInitializationError: Field '_email@49051279' has already been initialized.

When the exception was thrown, this was the stack
#0      LateError._throwFieldAlreadyInitialized (dart:_internal-patch/internal_patch.dart:201:5)
#1      _LoginViewState._email=
package:learn_flutter_37_hours/views/login.dart:14
#2      _LoginViewState.dispose
tareq albeesh
  • 1,701
  • 2
  • 10
  • 13

2 Answers2

0

In the dispose() method you are setting again the values of _email and _password:

@override
  void dispose() {
    _email = TextEditingController();
    _password = TextEditingController();
    super.dispose();
  }

Since both variables are final, you can only set their value once, as you do in initState. In fact you should dispose these controllers in the dispose() method and not set them again. Try this:

@override
  void dispose() {
    _email.dispose();
    _password.dispose();
    super.dispose();
  }
Peter Koltai
  • 8,296
  • 2
  • 10
  • 20
0

Note: Your dispose function should no be like that. You need to replace it with the following code

 @override
  void dispose() {
    _email.dispose();
    _password.dispose();
    super.dispose();
  }
Tanaka K
  • 1
  • 4