0

In this code :

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

Where doFancy() is declared as :

Future<something> doFancy() 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?

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;
        },
  • But the textfield expects a function, there is no reason for you to pass a future to it, if inside your validator you are executing asynchronous code, you must create a function of type string and add the async parameter and then work your methods asynchronous. If possible, add an example of this validator that uses asynchronous code. – Chance Feb 20 '21 at 22:29
  • Check my answer posted [here](https://stackoverflow.com/a/75685423/11217849). Maybe it will help you. – petomuro Mar 09 '23 at 19:17

2 Answers2

0

Just add the async keyword:

TextFormField(
        .
        .
        .
        validator: (value) async => doFancy();
      ),
Antonin GAVREL
  • 9,682
  • 8
  • 54
  • 81
0

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