Skip to content Skip to sidebar Skip to footer

Flutter - Async Validator Of Textformfield

In my app the user has to insert a name in the textformfield. While the user is writing a query should be made to the database, that controls if the name already exists. This query

Solution 1:

Perhaps you could run your async check using the onChange handler and set a local variable to store the result.

Something like:

TextFormField(
  controller: recipeDescription,
  decoration: InputDecoration(hintText: "Beschreibe dein Rezept..."),
  keyboardType: TextInputType.multiline,
  maxLines: null,
  maxLength: 75,
  onChanged: (text) async {
    final check = await checkRecipe(text);
    setState(() => hasRecipe = check);
  },
  validator: (_) => (hasRecipe) ? "Exists" : null,
)

Solution 2:

I wanted a same behavior for one of our apps and ended up writing a widget (which I recently published to pub.dev).

enter image description here

AsyncTextFormField(
    controller: controller,
    validationDebounce: Duration(milliseconds: 500),
    validator: isValidPasscode,
    hintText: 'Enter the Passcode')

You can pass in a Future<bool> function for the validator and set an interval before the text is sent to server.

The code is available on github.

Post a Comment for "Flutter - Async Validator Of Textformfield"