Home > Software design >  An email address must be provided / The email address is badly formatted
An email address must be provided / The email address is badly formatted

Time:10-17

I'm getting "flutter: [firebase_auth/missing-email] An email address must be provided." when I register. Used [email protected]

And "flutter: [firebase_auth/invalid-email] The email address is badly formatted." when I sign in with a registered account. (previously had an email that worked and made an account via firebase website also, get the same msg for both)

I saw trim() as a possible solution but don't know how to implement it. I made sure there were no spaces tho... can you please tell me what is wrong or missing from my code?

Here is some code from my register page (looks pretty much the same on my sign in page), let me know if you need to see more

  String email = '';
  String password = '';
  String error = '';
TextFormField(
                        textInputAction: TextInputAction.next,
                        style: TextStyle(color: Colors.white),
                        decoration: textInputDecoration.copyWith(
                          hintText: ('Enter Email'),
                          hintStyle: TextStyle(color: Colors.white),
                          enabledBorder: UnderlineInputBorder(
                            borderSide: BorderSide(color: Colors.white),
                          ),
                        ),
                        keyboardType: TextInputType.emailAddress,
                        validator: (value) {
                          if (value!.isEmpty) {
                            return 'Email is required';
                          }
                          if (value.isNotEmpty) {
                            final RegExp regex = RegExp(
                                r'^(([^<>()[\]\\.,;:\s@\"] (\.[^<>()[\]\\.,;:\s@\"] )*)|(\". \"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9] \.) [a-zA-Z]{2,}))$');
                            if (!regex.hasMatch(value))
                              return 'Enter a valid email';
                            else
                              return null;
                          } else {
                            return 'Enter a valid email';
                          }
                        },
                      ),
ElevatedButton(
                          child: Text(
                            'Register',
                            style: TextStyle(color: Colors.white),
                          ),
                          style: ElevatedButton.styleFrom(
                              primary: Color(0xFF162242)),
                          onPressed: () async {
                            if (_formKey.currentState!.validate()) {
                              setState(() => loading = true);
                              dynamic result =
                                  await _auth.registerWithEmailAndPassword(
                                      email, password);

                              if (result == null) {
                                setState(() {
                                  error = 'please enter valid email';
                                  loading = false;
                                });
                              }
                            }
                          }),

CodePudding user response:

Wait, I'm wondering what you're actually doing tho!

You have explicitly defined email, and password as empty strings.

 String email = '';
 String password = '';
 String error = '';

And passing those empty strings into _auth.registerWithEmailAndPassword(email, password)?

The solution would be to use TextEditingController objects and collect values from the input fields, then pass them to the registration method.

final emailCtrl =TextEditingController();
final passCtrl = TextEditingController();

...

TextFormField(
  controller: emailCtrl,
  ....
),

TextFormField(
  controller: passCtrl,
  ....
),

...


ElevatedButton(
  onPressed: () async {
    String email = emailCtrl.text.trim();
    String pass = passCtrl.text.trim();
    ...
    dynamic result = await _auth.registerWithEmailAndPassword(email, password);
    ...
  }
)
  • Related