我是Flatter中的初学者,问题是——我试图用validation和"Submit"按钮构建一个表单,该按钮必须导航到一个新屏幕,但当我按下该按钮时,Debugger中的"NoSuchMethodError:在null"上调用了"validate"方法.以下是代码:

class _SignInPage extends State<SignInPage> {
  final scaffoldKey = GlobalKey<ScaffoldState>();
  final formKey = GlobalKey<FormState>();

  String _email;
  String _password;

  void _submit() {
    final form = formKey.currentState;

    if (form.validate()) {
      form.save();
      _performLogin();
    }
  }

  void _performLogin() {
    Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => ConnectPage()),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: scaffoldKey,
      body: new Container(
          margin: new EdgeInsets.symmetric(vertical: 200.0, horizontal: 35.0),
          padding: EdgeInsets.all(20.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            key: formKey,
            children: [
              TextFormField(
                keyboardType: TextInputType.emailAddress,
                autofocus: false,
                decoration: InputDecoration(
                    contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
                    border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(20.0)),
                    labelText: 'Email'),
                validator: (val) =>
                    !val.contains('@') ? 'Not a valid email.' : null,
                onSaved: (val) => _email = val,
              ),
              TextFormField(
                autofocus: false,
                obscureText: true,
                decoration: InputDecoration(
                    hintText: 'Password',
                    contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
                    border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(20.0)),
                    labelText: 'Password'),
                validator: (val) =>
                    val.length < 6 ? 'Password too short.' : null,
                onSaved: (val) => _password = val,
              ),
              Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
                new Row(children: [
                  FlatButton(
                    child:
                        Text('Sign Up', style: TextStyle(color: Colors.black)),
                    onPressed: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(builder: (context) => SignUpPage()),
                      );
                    },
                  ),
                  FlatButton(
                    child:
                        Text('Sign In', style: TextStyle(color: Colors.black)),
                    onPressed: () {
                      // Navigate to second screen when tapped!
                    },
                  ),
                ]),
                new Row(children: [
                  Radio(
                    activeColor: Colors.blue,
                    value: Colors.grey,
                    onChanged: (activeColor) {},
                  ),
                  Text(
                    "Keep me signed in",
                    style: new TextStyle(color: Colors.black),
                  )
                ])
              ]),
              RaisedButton(
                color: Colors.blue,
                onPressed: _submit,
                child: new Text('Sign in'),
              ),
            ],
          )),
    );
  }
}
  1. 我怎么才能把它修好呢?
  2. 如何在两个表单之间添加空格?

推荐答案

您必须使用Form小部件将您的TextFormFields提高到wrap.

按每docs美元计算,

每个单独的表单域都应该包装在一个FormField小部件中,表单小部件作为所有这些*的共同祖先.

Dart相关问答推荐

Dartpad使用基本身份验证发出请求

dart中如何从xpath获取属性值?

自定义主题无法正常工作

Dart:使用正则表达式从字符串中删除空格

AppLifcycleState.didChangeLifecycleState( ) 函数在应用程序进入前台或后台时不被调用

如何在Flatter中导入intl库?

如何签署 Flutter 的应用程序

Flutter 以编程方式触发 FutureBuilder

如何在 Flutter 中管理 Firebase 身份验证状态?

在Flatter中导航后,无法关注新页面中的文本字段

不推荐使用AnteStorStateofType,请改用findAncestorStateOfType

dart2js代码比javascript快多少?

如何在 showModalBottomSheet 中设置状态

如何在 Dart 中上传文件?

如何用dart语言初始化超类变量?

谷歌的 Dart 编程语言的作用是什么?

Dart 是否有断点语句?

Dart 会在多核环境中并行执行隔离吗?

Dart lambda/shortland 函数混淆

在 Dart 中克​​隆list列表、map或set集合