How to check user email and password validation in firebase to login

4,044

Solution 1

Replying to the question further asked on the comments, to get your variables to be filled as they are typed and then correct when you call Firebase for auth, change your onSaved property in your TextFormFields to onChanged:

TextFormField(
  decoration: InputDecoration(
    labelText: 'Email'
  ),
  onChanged: (input) => _email = input,
),
TextFormField(
  decoration: InputDecoration(
    labelText: 'Password'
  ),
  onChanged: (input) => _password = input,
  obscureText: true,
),

Solution 2

enter image description here

you can change on onChanged properties

 onChanged: (value) => password = value,
Share:
4,044
ADev
Author by

ADev

Updated on December 16, 2022

Comments

  • ADev
    ADev over 1 year

    how to check user email and password validation in firebase to login to another page i have setup all things in firebase and dependencies in android studio

    signin.dart:

    import 'package:flutter/material.dart';
    import 'package:flutter_app/pages/home.dart';
    import 'package:firebase_auth/firebase_auth.dart';
    
    class LoginPage extends StatefulWidget {
      @override
      _LoginPageState createState() => new _LoginPageState();
    }
    
    class _LoginPageState extends State<LoginPage> {
      String _email, _password;
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(),
          body: Form(
              child: Column(
                children: <Widget>[
                  TextFormField(
                    decoration: InputDecoration(
                        labelText: 'Email'
                    ),
                    onSaved: (input) => _email = input,
                  ),
                  TextFormField(
                    decoration: InputDecoration(
                        labelText: 'Password'
                    ),
                    onSaved: (input) => _password = input,
                    obscureText: true,
                  ),
                  RaisedButton(
                    onPressed: loginUser,
                    child: Text('Sign in'),
                  ),
                ],
              )
          ),
        );
      }
    
      Future<FirebaseUser> loginUser() async {
        try {
           await FirebaseAuth.instance
              .signInWithEmailAndPassword(email: _email, password: _password);
          Navigator.push(context, MaterialPageRoute(builder: (context) => Home()));
          // since something changed, let's notify the listeners...
        }  catch (e) {
          // throw the Firebase AuthException that we caught
          throw new AuthException(e.code, e.message);
        }
      }
    }
    

    it gives me that

    E/flutter (20243): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: Class '_AssertionError' has no instance getter 'code'.
    E/flutter (20243): Receiver: Instance of '_AssertionError'
    E/flutter (20243): Tried calling: code
    E/flutter (20243): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
    E/flutter (20243): #1      _LoginPageState.loginUser (package:flutter_app/setup/signin.dart:51:33)
    E/flutter (20243): #2      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:706:14)
    E/flutter (20243): #3      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:789:36)
    E/flutter (20243): #4      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
    E/flutter (20243): #5      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:486:11)
    E/flutter (20243): #6      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:264:5)
    E/flutter (20243): #7      BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:199:7)
    E/flutter (20243): #8      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:467:9)
    E/flutter (20243): #9      PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:76:12)
    E/flutter (20243): #10     PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:117:9)
    

    E/flutter (20243): #11 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:379:8) E/flutter (20243): #12 PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:115:18)

    • J. S.
      J. S. over 4 years
      The error you are getting in your e variable doesn't seem to have a code method. Can you show us what is the output of your e variable?
    • J. S.
      J. S. over 4 years
      I don't understand your question. The variable I am speaking about is your error result in the catch that throws an AuthException. To be able to understand what is going on, we need to know what is the output of that error. Can you please try to print the error output before you throw the AuthException and add it to your question?
    • ADev
      ADev over 4 years
      I/flutter (25390): 'package:firebase_auth/src/firebase_auth.dart': Failed assertion: line 174 pos 12: 'email != null': is not true.
    • J. S.
      J. S. over 4 years
      That seems to be your first problem then. The email field that firebase expects is empty.
    • ADev
      ADev over 4 years
      i have changed it to signInWithEmailAndPassword(email: '[email protected]', password: '111111'); that is in firebase and it worked ,, now how can i make it take _email and _password variables ?
    • J. S.
      J. S. over 4 years
      Please check my answer to your question about the variables below, which is actually your original problem, if not the question itself. If correct, please mark the answer as correct.
  • ADev
    ADev over 4 years
    This is the cause of the problem from beginning , thanks very much