【问题标题】:Error message does not displayed after validation with firestore使用 firestore 验证后不显示错误消息
【发布时间】: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


    【解决方案1】:

    _isUsernameExists 是异步功能,您没有使用 await。而且返回的位置不正确。

     bool exist = await _isUsernameExists(input.toLowerCase().trim());
     if (exist) {
       print('exists');
       return msg_username_exists;
     }else{
       print('not exists');
       //return msg_username_not_exists;
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-07
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多