android - 验证程序未将数据保存到Firebase数据库

标签 android firebase flutter dart flutter-layout

我目前作为一个初学者正在做一个 flutter 的项目,我喜欢它。现在我开始接触后端(只是一个简单的CREATE WRITE btw),问题是当我输入数据时,它是Firebase中的null
我认为问题出在我的TextFormFields验证器中

                    TextFormField(
                      decoration: InputDecoration(hintText: 'Enter Offer Name'),
                      validator: (value) {
                        if (value.isEmpty) {
                        }
                        return 'Please Enter Offer Name';
                      },
                      onSaved: (value) => offerName = value,
                    ),

我似乎无法找到一种方法来解决此问题,它只能返回''内的文本,而不保存实际的输入,从而在Firebase数据库中显示null

Additional details: I want the forms to save its data to firebase after clicking this FlatButton


                      child: FlatButton(
                        color: Colors.blue,
                        child: Text("Confirm", style: TextStyle(color: Colors.white)),
                        onPressed: () async {
                          await db.collection("createdoffers").add(
                            {
                              'name': offerName,
                              'type': offerType,
                              'start': start,
                              'end': end,
                            }
                          );
                        }
                      ),

最佳答案

您代码中的return语句应位于if子句内,当前,由于return不在Please enter some text之外,因此它将始终返回if。因此,将validator函数更改为以下内容:

TextFormField(
  // The validator receives the text that the user has entered.
  validator: (value) {
    if (value.isEmpty) {
      return 'Please enter some text';
    }
    return null;
  },
);

Validate the input by providing a validator() function to the TextFormField. If the user’s input isn’t valid, the validator function returns a String containing an error message. If there are no errors, the validator must return null.

关于android - 验证程序未将数据保存到Firebase数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59412486/

相关文章:

dart - 空状态页面和导航器问题?

android - 从 google play 下载应用程序时找不到您尝试购买的商品

android - 从导航的深层链接调用我的应用程序会导致我的 fragment 被多次实例化

android - ViewStub 在有条件地扩充多个布局时引发错误

ios - Firebase 崩溃并出现 'listen() called twice for the same query' 错误

flutter - flutter 中是否有数字输入字段,该字段附有递增/递减按钮?

android - 使用 Android 相机应用程序以与启动时不同的方向拍照后应用程序崩溃

swift - 按时间顺序快速排序 Firebase 帖子

swift4 创建用户信息名称、年龄并从火力基地获取数据

android - Flutter Android 版本构建签名失败