如何使用_formKey验证TextFormField?

时间:2020-06-04 00:38:47

标签: flutter dart

我说错了

在处理手势时引发了以下NoSuchMethodError: 方法'validate'在null上被调用

我认为我需要包装TextFormFields,但是不确定如何执行/编码。

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"));
                              }
                            }))
                      ],
                    ))));
      }
    }

1 个答案:

答案 0 :(得分:1)

您可以在下面复制粘贴运行完整代码
您可以将ColumnForm一起使用,并将_formKey移至Form

代码段

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

工作演示

enter image description here

完整代码

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(),
    );
  }
}