简体   繁体   English

如何修复“类型'String'不是类型'(String?)=> String的子类型?” flutter 中的错误

[英]How to fix "type 'String' is not a subtype of type '(String?) => String?" error in flutter

I am learning flutter and getting so many errors with flutter...我正在学习 flutter 并在 flutter 中遇到很多错误......

I am trying to get a validation of text fields with the following methods:我正在尝试使用以下方法验证文本字段:

validateName() {
String value = '';
if (value.isEmpty) return 'Username is Required.';
final RegExp nameExp = new RegExp(r'^[A-za-zğüşöçİĞÜŞÖÇ ]+$');
if (!nameExp.hasMatch(value))
  return 'Please enter only alphabetical characters.';
return value;
}

However, I am getting the above mentioned error on validationFunction in the textformbuilder:但是,我在 textformbuilder 中的 validationFunction 上收到上述错误:

TextFormBuilder(
        enabled: !viewModel.loading,
        prefix: Feather.user,
        hintText: "Username",
        textInputAction: TextInputAction.next,
        validateFunction: validateName(),
        onSaved: (String val) {
          viewModel.setName(val);
        },
        focusNode: viewModel.usernameFN,
        nextFocusNode: viewModel.emailFN,
      ),

Thanks in advance!提前致谢!

validate function is different from your function...so change the function validate name验证 function 与您的 function 不同...所以更改 function 验证名称

  String validateName(String? value) {
   if (value!.isEmpty) return 'Username is Required.';
   final RegExp nameExp = new RegExp(r'^[A-za-zğüşöçİĞÜŞÖÇ ]+$');
   if (!nameExp.hasMatch(value))
   return 'Please enter only alphabetical characters.';
   return value;
  }

and on the textfield builder...put the validateFunction like this并在文本字段构建器上...像这样放置 validateFunction

TextFormBuilder(
 validateFunction: validateName,
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 type 'Null' is not a subtype of type 'String' 错误 in Flutter - type 'Null' is not a subtype of type 'String' error in Flutter 在 flutter 中解码字符串 json 时出错,“字符串类型不是 Map 类型的子类型” - Error decoding string json in flutter, “type String is not subtype of type Map” 如何解决抖动错误字符串不是int类型的子类型 - How to solve flutter error String is not a subtype of type int &#39;String&#39; 类型不是 &#39;index&#39; Flutter 的 &#39;int&#39; 类型的子类型 - type 'String' is not a subtype of type 'int' of 'index' Flutter “String”类型不是 Flutter 中“Item”类型的子类型 - type 'String' is not a subtype of type 'Item' in Flutter Flutter 中的错误:“字符串”类型不是“地图”类型的子类型<string, dynamic> ? 在类型转换中</string,> - Error in Flutter: type 'string' is not a subtype of type 'map<string, dynamic>?' in type cast Flutter。类型“Null”不是类型转换中类型“String”的子类型 - Flutter. type 'Null' is not a subtype of type 'String' in type cast “String”类型不是“FontStyle”类型的子类型 - type 'String' is not a subtype of type 'FontStyle' “时间戳”类型不是“字符串”类型的子类型 - type 'Timestamp' is not a subtype of type 'String' “字符串”类型不是“列表”类型的子类型<latlng> ' 我如何将闲置的 flutter 字符串转换为 LatLng 以使用 Polygon</latlng> - type 'String' is not a subtype of type 'List<LatLng>' how do i convert the fallowing flutter string to LatLng for the use of Polygon
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM