【发布时间】:2019-12-12 07:07:45
【问题描述】:
我正在尝试验证我的用户名是否已存在于 firestore 数据库中。如果它已经存在,它将在文本框中显示一条消息。通过查看打印的值,我已成功检查了 firestore 中的值。但不知何故,用户名存在时的消息没有显示。知道为什么吗?
检查用户名是否存在于firestore中:
Future<bool> _isUsernameExists(String username) async {
final QuerySnapshot result = await Firestore.instance
.collection('users')
.where('username', isEqualTo: username)
.limit(1)
.getDocuments();
final List<DocumentSnapshot> documents = result.documents;
return documents.length == 1;
}
用户名文本框:
final _username = Container(
child: TextFormField(
decoration: InputDecoration(labelText: username),
validator: (input) {
if (input.isEmpty) {
return msg_usermame_empty;
} else {
_isUsernameExists(input.toLowerCase().trim()).then((onValue) {
if (onValue) {
print('exists');
return msg_username_exists;
}else{
print('not exists');
}
});
}
},
onSaved: (input) => _stringUsername = input.toLowerCase().trim(),
),
);
return Scaffold(
appBar: CustomAppBar(text: sign_up),
body: Form(
key: _formKey,
child: SingleChildScrollView(
padding: EdgeInsets.fromLTRB(30.0, 20.0, 30.0, 0.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
_fullName,
SizedBox(height: 10.0),
_username,
SizedBox(height: 10.0),
_email,
SizedBox(height: 10.0),
_password,
SizedBox(height: 20.0),
_signUpButton,
SizedBox(height: 10.0),
],
),
)),
);
【问题讨论】:
标签: firebase flutter dart google-cloud-firestore