在TextFormField验证器Flutter中调用异步函数 [英] Call async function inside TextFormField validator Flutter

查看:172
本文介绍了在TextFormField验证器Flutter中调用异步函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此代码中:

TextFormField(
        .
        .
        .
        validator: (value) => doFancy();
      ),

doFancy()声明为:

Where doFancy() is declared as :

Future<something> doFancy() async{...

我如何在 TextFormField validator 内调用 doFancy(),因为它是 Future 函数,并且我不能将 validator 声明为 async 吗?

How can I call doFancy() inside the validator of a TextFormField since it's a Future function and I can't declare the validator as async?

这是我所能达到的最好水平,但是我仍然遇到相同的问题.参数类型'Future< String>不能将Function(String)'分配给参数类型'String Function(String)'.

EDIT : This is the best that I reached but I still have the same issue 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;
        },

推荐答案

解决了移出异步函数并使用setState中处理的变量的问题.

Solved moving out the async function and working with variables handled in setState .

这篇关于在TextFormField验证器Flutter中调用异步函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆