user5889211
user5889211

Reputation:

How to validate a TextFormField using _formKey?

I'm getting an error saying

The following NoSuchMethodError was thrown while handling a gesture: The method 'validate' was called on null

I believe I need to wrap TextFormFields but unsure of how to do/code this.

import 'package:firebase_auth/firebase_auth.dart';
    import 'package:flutter/material.dart';
    import '../../shared/constants.dart';

    class PasswordReset extends StatefulWidget {
      @override
      _PasswordResetState createState() => _PasswordResetState();
    }

    class _PasswordResetState extends State<PasswordReset> {
      final _formKey = GlobalKey<FormState>();
      String email = '';
      String error = '';

      @override
      Widget build(BuildContext context) {
        return Scaffold(
            backgroundColor: Colors.purple[200],
            appBar: AppBar(
              backgroundColor: Colors.purple[800],
              title: Text('Reset Password'),
            ),
            body: Center(
                key: _formKey,
                child: Padding(
                    padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 50.0),
                    child: Column(
                      children: <Widget>[
                        TextFormField(
                            decoration:
                                textInputDecoration.copyWith(hintText: 'Email'),
                            validator: (value) {
                              if (value.isEmpty) {
                                return "Please enter your email";
                              } else {
                                email = value;
                              }
                              return null;
                            }),
                        Padding(
                            padding: EdgeInsets.symmetric(
                                vertical: 20.0, horizontal: 50.0),
                            child: RaisedButton(
                              color: Colors.blue[400],
                              child: Text('submit',style: TextStyle(color: Colors.white)),
                              onPressed: () {
                              if (_formKey.currentState.validate()) {
                                FirebaseAuth.instance
                                    .sendPasswordResetEmail(email: email)
                                    .then((value) => print("Check inbox"));
                              }
                            }))
                      ],
                    ))));
      }
    }

Upvotes: 0

Views: 436

Answers (1)

chunhunghan
chunhunghan

Reputation: 54377

You can copy paste run full code below
You can use wrap Column with Form and move _formKey to Form

code snippet

Center(
          child: Padding(
              padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 50.0),
              child: Form(
                key: _formKey,
                child: Column(

working demo

enter image description here

full code

import 'package:flutter/material.dart';

class PasswordReset extends StatefulWidget {
  @override
  _PasswordResetState createState() => _PasswordResetState();
}

class _PasswordResetState extends State<PasswordReset> {
  final _formKey = GlobalKey<FormState>();
  String email = '';
  String error = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.purple[200],
        appBar: AppBar(
          backgroundColor: Colors.purple[800],
          title: Text('Reset Password'),
        ),
        body: Center(
          child: Padding(
              padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 50.0),
              child: Form(
                key: _formKey,
                child: Column(
                  children: <Widget>[
                    TextFormField(
                        /*decoration:
                            textInputDecoration.copyWith(hintText: 'Email'),*/
                        validator: (value) {
                      if (value.isEmpty) {
                        return "Please enter your email";
                      } else {
                        email = value;
                      }
                      return null;
                    }),
                    Padding(
                        padding: EdgeInsets.symmetric(
                            vertical: 20.0, horizontal: 50.0),
                        child: RaisedButton(
                            color: Colors.blue[400],
                            child: Text('submit',
                                style: TextStyle(color: Colors.white)),
                            onPressed: () {
                              if (_formKey.currentState.validate()) {
                                /*FirebaseAuth.instance
                                    .sendPasswordResetEmail(email: email)
                                    .then((value) => print("Check inbox"));*/
                              }
                            }))
                  ],
                ),
              )),
        ));
  }
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: PasswordReset(),
    );
  }
}

Upvotes: 1

Related Questions