TextFormField验证返回null

时间:2020-06-21 21:57:00

标签: flutter

我想知道为什么在尝试对表单进行验证时出现问题。我遇到的错误是NoSuchMethodError。调用key.currentState.validate()时,可以用任何键值替换key

final _formKey = GlobalKey();
final _nameKey = GlobalKey();
final _emailKey = GlobalKey();
final _passwordKey = GlobalKey();
final _passwordConfirmKey = GlobalKey();
final _dobKey = GlobalKey();
final _fullNameController = TextEditingController();
final _emailController = TextEditingController();
final _passwordController = TextEditingController();
final _passwordConfirmController = TextEditingController();
final _dateOfBirthController = TextEditingController();

  Form(
    key: _formKey,
    autovalidate: false,
    child: Column(
      children: <Widget>[
        loginSignupTextForm(_nameKey, _fullNameController, "Name",
            hintText: "John Doe",
            prefixIcon: Icons.person,
            validator: validateName),
        SizedBox(height: 15.0),
        loginSignupTextForm(_emailKey, _emailController, "Email",
            autoValidate: false,
            hintText: "example@email.com",
            prefixIcon: Icons.email,
            suffixIcon: !_validEmailAddress ? Icon(Icons.error, color: Colors.white) : null,
            validator: validateEmail),

从另一个文件:

Widget loginSignupTextForm(Key key, TextEditingController controller, String label,
    {bool autoValidate = false,
    bool readOnly = false,
    bool showCursor = true,
    TextInputType keyboardType,
    IconData prefixIcon,
    StatelessWidget suffixIcon,
    String hintText = "",
    bool obscureText = false,
    Function onTap,
    List functionParamters,
    Function validator,
    Function onSaved,
    Function onChanged}) {
  return Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: <Widget>[
      Text(
        label,
        style: labelStyle,
      ),
      SizedBox(height: 10.0),
      Container(
        alignment: Alignment.centerLeft,
        decoration: boxDecorationStyle,
        height: 60.0,
        child: TextFormField(
          key: key,
          autovalidate: autoValidate,
          readOnly: readOnly,
          showCursor: showCursor,
          onChanged: (value) => onChanged != null ? onChanged(value) : null,
          onSaved: (newValue) => onSaved != null ? onSaved(newValue) : null,
          validator: (value) => validator != null ? validator(value) : null,
          onTap: () =>
              onTap != null ? Function.apply(onTap, functionParamters) : null,
          maxLines: 1,
          controller: controller,
          obscureText: obscureText,
          keyboardType: keyboardType != null ? keyboardType : null,
          style: TextStyle(
            color: Colors.white,
            fontFamily: 'OpenSans',
          ),
          decoration: InputDecoration(
            border: InputBorder.none,
            contentPadding: EdgeInsets.only(top: 14.0),
            prefixIcon: prefixIcon != null
                ? Icon(
                    prefixIcon,
                    color: Colors.white,
                  )
                : null,
            suffixIcon: suffixIcon != null ? suffixIcon : null,
            hintText: hintText,
            hintStyle: hintTextStyle,
          ),
        ),
      ),
    ],
  );
}

我对格式感到抱歉,似乎无法弄清为什么换行符不起作用。 方法loginSignupTextForm建立了一个TextFormField,并且keys确实通过了。

1 个答案:

答案 0 :(得分:0)

找出问题,我将错误的类型传递给GlobalKey。正确的类型是FormFieldState。