TextFormField 验证器,错误在 TextFormField 上不可见

TextFormField validator, Error not visible on TextFormField

我正在尝试创建一个带有列的表单。

点赞,表单 => 列 => 文本字段,

TextFields 将有验证器。 此外,TextFields 将进行装饰。

但是当我点击RaisedButton时,我找不到TextField上的错误。 虽然TextFormField中的return执行成功了。 (我使用调试器检查过)

我目前参考的链接如下: https://www.youtube.com/watch?v=xiEYNzU4bDg

https://iirokrankka.com/2017/10/17/validating-forms-in-flutter/

https://medium.com/@nitishk72/form-validation-in-flutter-d762fbc9212c

下面是我的代码,请求帮助我解决这个问题

import 'package:flutter/material.dart';

class Profile extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new ProfileState();
  } //createState closes here....
} //Profile closes here.....

class ProfileState extends State<Profile> {
  @override
  Widget build(BuildContext context) {
    final _formKey = GlobalKey<FormState>();

    return new Scaffold(
        appBar: new AppBar(title: new Text("Profile")),
        body: Form(
          key: _formKey,
          child: new Column(
            children: <Widget>[
              //Full Name TextGormFeild goes below....
              new TextFormField(
                  validator: (String value) {
                    if (value.isEmpty) {
                      return "Full name cannot be empty.";//Control comes here when I check using the Debugger.
                    } //if(value.isEmpty) closes here....
                  },
                  keyboardType: TextInputType.text,
                  textInputAction: TextInputAction.next,
                  maxLines: 1,
                  decoration: InputDecoration(
                      border: new OutlineInputBorder(
                          borderSide: new BorderSide(color: Colors.grey)),
                      hintText: "Enter name")),

              new RaisedButton(
                child: new Text("Test"),
                onPressed: () {
                  setState(() {
                    if (_formKey.currentState.validate()) {
                      print("Validations are correct.");
                    }
                  });
                },
              )
            ],
          ),
        )

        //,
        // helperText: "Enter full name"
        );
  } //build closes here.....

} //ProfileState closes here....

之前我使用的是 helperText,但我已经删除了它。

尝试从您的 onPressed

中删除 setState
setState(() {

});

只放

if (_formKey.currentState.validate()) {
  print("Validations are correct.");
}

如下所示:

onPressed: () {
    if (_formKey.currentState.validate()) {
      print("Validations are correct.");
    }
},