颤振异步验证表单 [英] flutter validate form asynchronously
本文介绍了颤振异步验证表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
new TextFormField(
validator: (value) async{
if (value.isEmpty) {
return 'Username is required.';
}
if (await checkUser()) {
return 'Username is already taken.';
}
},
controller: userNameController,
decoration: InputDecoration(hintText: 'Username'),
),
我有表单供用户使用,我想检查该用户是否已在Firestore数据库中。
I have a form for user, and I want to check if the user already exists in the firestore datebase.
Future checkUser() async {
var user = await Firestore.instance
.collection('users')
.document(userNameController.text)
.get();
return user.exists;
}
这是我的功能检查用户文档是否已存在于数据库中。
但是验证器却给我这个错误。
This is my function to check if the user document already exists in the database. But validator gives me this error.
[dart]参数类型'(String)→Future'不能为
[dart] The argument type '(String) → Future' can't be assigned to the parameter type '(String) → String'.
我该如何解决此问题?
推荐答案
目前,我认为您不能将未来
与验证器
。
At this time I think that you can't associate a Future
to a validator
.
您可以做的是通过单击按钮或以其他方式验证数据并设置验证器的状态
What you can do is this verifying the data on a button click or in another way and set the state on the validator response var.
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
body: Form(
key: _formKey,
child: Column(children: [
new TextFormField(
validator: (value) {
return usernameValidator;
},
decoration: InputDecoration(hintText: 'Username')),
RaisedButton(
onPressed: () async {
var response = await checkUser();
setState(() {
this.usernameValidator = response;
});
if (_formKey.currentState.validate()) {}
},
child: Text('Submit'),
)
])));
}
这篇关于颤振异步验证表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文