TextFormField validate 参数采用一个函数,如果字段内容有效则返回 null,如果内容无效则返回字符串。我的flutter项目中有 null 安全性,我无法从我的 validate 函数中返回 null。如何编写一个启用空安全的有效验证函数?
AppWidget.buildUserInput(
hint: 'Referral code',
borderRadius: BorderRadius.circular(5),
prefixIcon: Icon(Icons.local_offer_outlined),
onSaved: (val) => model.save(
CreateAccountViewModel.REFERRAL_CODE, val!),
validate: (val) {
return null; // The return type 'Null' isn't a 'String', as required by the
//closure's context
},
),
这是 buildUser 输入代码
static Widget buildUserInput(
{String? hint,
String? labelText,
TextAlign textAlign = TextAlign.start,
required Function(String? val) onSaved,
Function(String val)? onChanged,
required String Function(String val) validate,
TextInputType? keyboardType,
BorderRadius borderRadius = BorderRadius.zero,
TextEditingController? controller,
String? initialValue,
Widget? suffixIcon,
Widget? prefixIcon,
String? prefixText,
bool hasBorder = true,
Widget? prefix,
bool filled = false,
Color? fillColor,
Color? enabledBorderColor,
Color? focusedBorderColor,
TextStyle? style,
bool obscureText = false}) {
return Container(
margin: EdgeInsets.symmetric(vertical: 9),
child: TextFormField(
initialValue: initialValue,
controller: controller,
onSaved: onSaved,
onChanged: onChanged,
validator: validate,
keyboardType: keyboardType,
obscureText: obscureText,
textAlign: textAlign,
style: style ?? TextStyle(),
decoration: InputDecoration(
filled: filled,
fillColor: fillColor,
prefixText: prefixText, prefix: prefix,
prefixStyle: TextStyle(color: AppColors.appGreen),
suffixIcon: suffixIcon,
prefixIcon: prefixIcon,
hintText: hint,
labelText: labelText,
labelStyle: userInputStyle(),
hintStyle: userInputStyle(),
// filled: true,
// fillColor: AppColors.textColor2.withOpacity(.05),
border: hasBorder
? borderRadius != null
? OutlineInputBorder(
borderRadius: borderRadius,
borderSide: BorderSide(
style: BorderStyle.solid,
color: enabledBorderColor ?? AppColors.appGreen,
width: 1))
: UnderlineInputBorder(
borderRadius: borderRadius,
borderSide: BorderSide(
style: BorderStyle.solid,
color: enabledBorderColor ?? AppColors.appGreen,
width: 1))
: InputBorder.none,
focusedBorder: hasBorder
? borderRadius != null
? OutlineInputBorder(
borderRadius: borderRadius,
borderSide: BorderSide(
style: BorderStyle.solid,
color: focusedBorderColor ?? AppColors.appGreen,
width: 1))
: UnderlineInputBorder(
borderRadius: borderRadius,
borderSide: BorderSide(
style: BorderStyle.solid,
color: focusedBorderColor ?? AppColors.appGreen,
width: 1))
: InputBorder.none,
enabledBorder: hasBorder
? borderRadius != null
? OutlineInputBorder(
borderRadius: borderRadius,
borderSide: BorderSide(
style: BorderStyle.solid,
color: enabledBorderColor ?? AppColors.appGreen,
width: 1))
: UnderlineInputBorder(
borderRadius: borderRadius,
borderSide: BorderSide(
style: BorderStyle.solid,
color: enabledBorderColor ?? AppColors.appGreen,
width: 1))
: InputBorder.none,
),
),
);
}
}
问题不清楚。所以如果你想使用 Flutter Form()。您需要一个表单键 = GlobalKey()。如果你想在无效时得到警告,你不能返回''。当您的值无效时,它将有一个红色警告。