【发布时间】:2021-05-23 14:14:46
【问题描述】:
在这段代码中:
TextFormField(
.
.
.
validator: (value) => doFancy();
),
doFancy() 被声明为:
Future<something> doFancy() async{...
我如何在TextFormField 的validator 中调用doFancy(),因为它是Future 函数,我不能将validator 声明为async?
编辑:
这是我达到的最好的,但我仍然有同样的问题The argument type 'Future<String> Function(String)' can't be assigned to the parameter type 'String Function(String)'。
validator: (value) async {
return (await checkMissingId(value, context) == false)
? "Username already taken"
: null;
},
【问题讨论】:
-
但是文本字段需要一个
函数,你没有理由向它传递一个未来,如果在你的验证器中你正在执行异步代码,你必须创建一个字符串类型的函数并且添加 async 参数,然后异步处理您的方法。如果可能,请添加一个使用异步代码的验证器示例。
标签: flutter dart asynchronous