我目前作为一个初学者正在做一个 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/