验证程序未将数据保存到Firebase数据库 [英] Validator not saving data to Firebase database
问题描述
目前,我是一个初学者,正在做一个扑扑的项目,我喜欢它。现在我开始接触后端(只是一个简单的CREATE WRITE顺便说一句),问题是当我输入数据时,它是Firebase中的 null
im currently making a flutter project as a beginner and im kind of loving it. Now Im beginning to touch the backend (just a simple CREATE WRITE btw) the problem is when I input my data, it is null
in Firebase
我认为问题出在我的 TextFormFields
验证器
I think the problem is here in my TextFormFields
validator
TextFormField(
decoration: InputDecoration(hintText: 'Enter Offer Name'),
validator: (value) {
if (value.isEmpty) {
}
return 'Please Enter Offer Name';
},
onSaved: (value) => offerName = value,
),
我似乎无法找到一种方法来解决这个问题,只返回内的文本 >并且不保存 actual 输入,因此在Firebase数据库中显示
null
I cant seem to find a way to fix this one up its only returning the text inside ''
and not saving the actual input hence showing a null
in Firebase Database
其他详细信息:我要单击此
FlatButton
$ b之后的表单,即可将其数据保存到firebase $ b
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子句内,当前它将始终返回请输入一些文本
,因为返回值不在 if
。因此,将 validator
函数更改为以下内容:
The return statement in your code should be inside the if clause, currently it will always return Please enter some text
since the return is outside the if
. Therefore change the validator
function to the following:
TextFormField(
// The validator receives the text that the user has entered.
validator: (value) {
if (value.isEmpty) {
return 'Please enter some text';
}
return null;
},
);
通过为TextFormField提供Validator()函数来验证输入。如果用户输入的内容无效,验证器函数将返回一个包含错误消息的字符串。如果没有错误,则验证器必须返回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.
这篇关于验证程序未将数据保存到Firebase数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!