I'm following this https://medium.com/@c_innovative/simple-firebase-login-flow-in-flutter-6f44c2b5c58a tutorial and i am having some trouble with getting the login in to work. I have already connected my app to firebase and enabled email and password authentication on firebase console!

I have tried entering the key argument into the TextFormfield but so far im unable to do so.

class LoginPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _LoginPageState();
  }
}

class _LoginPageState extends State<LoginPage> {
  final _formKey = GlobalKey<FormState>();
  String _email;
  String _password;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Login Page Flutter Firebase"),
        ),
        body: Container(
          padding: EdgeInsets.all(20.0),
          child: Column(
            children: <Widget>[
              Spacer(flex: 5),
              Text(
                "Login  Information",
                style: new TextStyle(fontSize: 30.0),
              ),
              Spacer(
                flex: 5,
              ),
              TextFormField(
                key: _formKey,
                onSaved: (value) => _email = value,
                keyboardType: TextInputType.emailAddress,
                decoration: InputDecoration(labelText: "Email Address"),
              ),
              TextFormField(
                onSaved: (value) => _password = value,
                obscureText: true,
                decoration: InputDecoration(labelText: "Password"),
              ),
              Spacer(flex: 3),
              RaisedButton(
                  child: Text("Login"),
                  onPressed: () async {
                    //Getting the error here
                    final form = _formKey.currentState;
                    form.save();

                    if (form.validate()) {
                      print('$_email $_password');
                      var result = await 
                      Provider.of<AuthService(context).loginUser(email: _email, password:_password);
                      if (result == null) {
                        print("result is null");
                      }
                    }
                  }),
              Spacer(
                flex: 20,
              )
            ],
          ),
        ));
  }
}

[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The method 'save' was called on null. Receiver: null Tried calling: save()

this error message is what i get. somehow i understand that i have to initialise the receiver object(?) but i am confused on how to do so. Thanks for the help!

1 Answers

0
Adrian Murray On

Instead of using onSaved I'd just use TextEditingControllers on your fields and get the values of the text from those when you call your signIn function.