在我的应用程序中,用户必须在文本表单字段中插入名称。当用户编写查询时,应该对数据库进行查询,这控制该名称是否已存在。该查询返回该名称存在的次数。到目前为止,我只要按下按钮就可以做到。
这是返回名称计数的函数:
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) 我在Flutter中有一个异步函数,它将验证器的值作为参数:
validatePhone(number) {
bool _isValid;
Requests.get("http://apilayer.net/api/validate?value=$number", json: true)
.then((val) {
if (val['valid']) {
// setState(() { <- also tried setting state here
_isValid = true;
// });
} else {
// setState(() {
_isValid = false;
// });
}
});
return _isValid;
}
Run Code Online (Sandbox Code Playgroud)
和
TextFormField(
validator: (value) {
if (value.isEmpty) {
return 'Please your phone number';
} else {
if (validatePhone(value)) {
return 'Your phone number is not valid';
}
}
},
),
Run Code Online (Sandbox Code Playgroud)
但它不起作用,它总是返回null或在 中设置的初始值validatePhone …
我有一个名为的收藏company。所有公司都将像我的屏幕快照中那样存储。
当我添加其他公司时,我想检查是否name已经存在。如何执行?
在这里,“ Nova”和“ Tradetech”是两家公司。
当我尝试再次在该字段中添加“ Nova”name: "nova"时,我想显示一个通知:“公司已经存在!” 。