TextFormField 验证器参数
TextFormField validator The argument
我正在尝试制作自定义小部件 TextFormField,但验证器:验证器,显示错误
参数类型dynamic Function()
无法分配给参数类型'String? Function(String?)?'.dartargument_type_not_assignable dynamic Function()
验证程序包:store/widgets/mytextformfield.dart
mytextformfield.dart <<< 文件小部件 TextFormField
import 'package:flutter/material.dart';
class MyTextFormField extends StatelessWidget {
final Function() validator;
final bool obserText;
final String name;
const MyTextFormField({
Key? key,
required this.obserText,
required this.validator,
required this.name,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return TextFormField(
obscureText: obserText,
**validator: validator,**
decoration: InputDecoration(
border: const OutlineInputBorder(),
hintText: name,
),
);
}
}
login.dart
MyTextFormField(
obserText: obserText,
name: "Email",
validator: (value) {
if (value == "") {
return "Please Fill Email";
} else if (!regExp.hasMatch(value!)) {
return "Email is Invaild";
}
return "";
},
)
尝试仅定义为 Function
而不是 Function()
:
final Function validator;
替换此行:
final Function() validator;
收件人:
final FormFieldValidator<String>? validator;
变化:
final Function() validator;
与:
final FormFieldValidator<String>? validator
正如在 TextFormField class
中声明的那样
我正在尝试制作自定义小部件 TextFormField,但验证器:验证器,显示错误
参数类型dynamic Function()
无法分配给参数类型'String? Function(String?)?'.dartargument_type_not_assignable dynamic Function()
验证程序包:store/widgets/mytextformfield.dart
mytextformfield.dart <<< 文件小部件 TextFormField
import 'package:flutter/material.dart';
class MyTextFormField extends StatelessWidget {
final Function() validator;
final bool obserText;
final String name;
const MyTextFormField({
Key? key,
required this.obserText,
required this.validator,
required this.name,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return TextFormField(
obscureText: obserText,
**validator: validator,**
decoration: InputDecoration(
border: const OutlineInputBorder(),
hintText: name,
),
);
}
}
login.dart
MyTextFormField(
obserText: obserText,
name: "Email",
validator: (value) {
if (value == "") {
return "Please Fill Email";
} else if (!regExp.hasMatch(value!)) {
return "Email is Invaild";
}
return "";
},
)
尝试仅定义为 Function
而不是 Function()
:
final Function validator;
替换此行:
final Function() validator;
收件人:
final FormFieldValidator<String>? validator;
变化:
final Function() validator;
与:
final FormFieldValidator<String>? validator
正如在 TextFormField class
中声明的那样