在我的应用程序中,用户必须在文本表单字段中插入名称。当用户编写查询时,应该对数据库进行查询,这控制该名称是否已存在。该查询返回该名称存在的次数。到目前为止,我只要按下按钮就可以做到。
这是返回名称计数的函数:
checkRecipe(String name) async{
await db.create();
int count = await db.checkRecipe(name);
print("Count: "+count.toString());
if(count > 0) return "Exists";
}
Run Code Online (Sandbox Code Playgroud)
这是 TextFormField,应该异步验证:
TextField(
controller: recipeDescription,
decoration: InputDecoration(
hintText: "Beschreibe dein Rezept..."
),
keyboardType: TextInputType.multiline,
maxLines: null,
maxLength: 75,
validator: (text) async{ //Returns an error
int count = await checkRecipe(text);
if (count > 0) return "Exists";
},
)
Run Code Online (Sandbox Code Playgroud)
代码的错误是:
参数类型 Future 不能分配给参数类型 String
我确实知道该错误意味着什么。但我不知道如何解决可能看起来像。如果有人能帮助我,那就太好了。
我的代码现在看起来像这样:
//My TextFormField validator
validator: (value) => checkRecipe(value) ? "Name already taken" : null, …Run Code Online (Sandbox Code Playgroud)