Flutter - Async Validator of TextFormField

前端 未结 1 1276
盖世英雄少女心
盖世英雄少女心 2021-01-14 08:45

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. Thi

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-14 09:30

    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,
    )
    

    0 讨论(0)
提交回复
热议问题