Android Firebase数据库中TextFormFields中的数据为空
我很难弄明白这一点(顺便说一句,我对Flitter还是新手),我用表单创建了一个新屏幕,让用户填写信息,填写信息后,有一个Android Firebase数据库中TextFormFields中的数据为空,android,flutter,flutter-layout,flutter-dependencies,mobile-development,Android,Flutter,Flutter Layout,Flutter Dependencies,Mobile Development,我很难弄明白这一点(顺便说一句,我对Flitter还是新手),我用表单创建了一个新屏幕,让用户填写信息,填写信息后,有一个验证器和保存:on每个TextFormField()截至目前,我只希望textform字段将数据保存到Firebase数据库 编辑:我使用此代码设法使其正常工作,但我在Firebase数据库中输入的数据为空(第二张图片): 我也看了一些教程,但我很难让它工作,因为它与我试图做的有点不同(我想这是一个初学者的问题,我是编程新手,我爱上了flutter lol) 现在,在Fire
验证器和保存:
on每个TextFormField()
截至目前,我只希望textform字段将数据保存到Firebase数据库
编辑:我使用此代码设法使其正常工作,但我在Firebase数据库中输入的数据为空
(第二张图片):
我也看了一些教程,但我很难让它工作,因为它与我试图做的有点不同(我想这是一个初学者的问题,我是编程新手,我爱上了flutter lol)
现在,在Firebase控制台上,我创建了一个新集合,其中包含一些新的
只是为了填充虚拟数据(请注意,我仍然不保存来自
应用程序,只是创建了一个集合并放入了一些虚拟数据)
我的firebase的图像如下所示:
为空
数据
下面是屏幕窗体的代码,我正试图通过单击FlatButton
我的目标是:1。将数据保存到firebase 2。读取数据并将其显示到容器小部件,我现在只需要CRUD中的C和R
出了什么问题
child:FlatButton(
颜色:颜色,蓝色,
子项:文本(“确认”,样式:文本样式(颜色:Colors.white)),
onPressed:()异步{
等待数据库集合(“createdoffers”)。添加(
{
“name”:offerName,//这是null,请尝试更改通过setState保存数据的方式
“类型”:offerType,
“开始”:开始,
“结束”:结束,
}
);
},
),
你能做什么
如果要继续此实现,请尝试textededitingcontroller
//声明
TextEditingController _offerNameExtController=TextEditingController();
//用法
TextFormField(
控制器:_offerNameExtController,
...
)
//从输入字段检索数据
扁平按钮(
已按下:(){
//利用该值(例如,在Firebase保存方法上
打印(offerNameTextController.text);
}
)
进一步阅读
您在保存过程中传递了textformfield
的值。但是您忘记了保存表单。添加此\u formKey.currentState.save()
请尝试更改保存的onsave
回调中变量的分配方式好吗?在setState
函数中移动offerName=value
。请告诉我这是否有帮助。谢谢。您好,我尝试过setState方法,但仍然不起作用。我将它添加到了我所做的@KDC中。我面临着同样的问题你能告诉我你是怎么解决的吗?嗨,我试过setState方法,但仍然不起作用。我把它添加到我所做的中了当然。但是你试过上面的这个实现吗?还没有,但我想我很快就明白了。我想问题在于我的TextFormField``TextFormField(装饰:输入装饰(hintText:'输入要约名称'),验证器:(值){if(value.isEmpty){}返回'请输入要约名称';},保存:(值)=>offerName=value,),``如果我能正确读取它,它只返回一个值,而不是实际保存它。只返回(“”)中的文本但是您是否尝试过打印TextFormField中的值?是否可以检查它?使用TextEditingController
,以便您可以从TextFormField
中检索值text
。
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,
}
);
},
),
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
class AddOffer extends StatefulWidget {
AddOffer({Key key}) : super(key: key);
@override
_AddOfferState createState() => _AddOfferState();
}
class _AddOfferState extends State<AddOffer> {
String offerName;
String offerType;
String start;
String end;
bool allBranches = false;
bool selectedBranches = false;
final db = Firestore.instance;
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: ListView(
children: <Widget>[
Container(
color: Color(0xFF707070),
height: 200.0,
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
InkWell(
onTap: () {
setState(() {
Navigator.pop(context);
});
},
child: Padding(
padding: EdgeInsets.fromLTRB(20, 30, 20, 0),
child: Icon(Icons.arrow_back,
color: Colors.white, size: 25.0),
),
),
Center(
child: Padding(
padding: EdgeInsets.all(80.0),
child: Text(
"DEAL IMAGE",
style: TextStyle(
fontSize: 20.0,
color: Colors.white,
fontWeight: FontWeight.bold),
),
),
),
],
),
),
Form(
key: _formKey,
child: Padding(
padding: EdgeInsets.fromLTRB(30, 30, 30, 0),
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Text(
"Name",
style: TextStyle(
color: Color(0xFF707070),
fontSize: 17.0,
fontWeight: FontWeight.bold),
),
],
),
TextFormField(
decoration: InputDecoration(hintText: 'Enter Offer Name'),
validator: (value) {
if (value.isEmpty) {
}
return 'Please Enter Offer Name';
},
onSaved: (value) => offerName = value,
),
SizedBox(height: 30.0),
Row(
children: <Widget>[
Text(
"Type",
style: TextStyle(
color: Color(0xFF707070),
fontSize: 17.0,
fontWeight: FontWeight.bold),
),
],
),
TextFormField(
decoration: InputDecoration(hintText: 'Enter Offer Type'),
validator: (value) {
if (value.isEmpty) {
}
return 'Please Enter Offer Type';
},
onSaved: (value) => offerType = value,
),
SizedBox(height: 60.0),
Row(
children: <Widget>[
Text(
"Start",
style: TextStyle(
color: Color(0xFF707070),
fontSize: 17.0,
fontWeight: FontWeight.bold),
),
],
),
TextFormField(
decoration:
InputDecoration(hintText: 'Enter Offer Start Date'),
validator: (value) {
if (value.isEmpty) {
}
return 'Please Enter Offer Start Date';
},
onSaved: (value) => offerName = value,
),
SizedBox(height: 30.0),
Row(
children: <Widget>[
Text(
"End",
style: TextStyle(
color: Color(0xFF707070),
fontSize: 17.0,
fontWeight: FontWeight.bold),
),
],
),
TextFormField(
decoration:
InputDecoration(hintText: 'Enter Offer End Date'),
validator: (value) {
if (value.isEmpty) {
}
return 'Please Enter Offer End Date';
},
onSaved: (value) => offerName = value,
),
SizedBox(height: 60.0),
Row(
children: <Widget>[
Column(
children: <Widget>[
Text(
"Valid Until",
style: TextStyle(
color: Color(0xFF707070),
fontSize: 17.0,
fontWeight: FontWeight.bold),
),
SizedBox(height: 5.0),
Text(
"01/01/20",
style: TextStyle(
color: Color(0xFF707070), fontSize: 17.0),
),
],
),
],
),
SizedBox(height: 30.0),
Row(
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"Time of Active",
style: TextStyle(
color: Color(0xFF707070),
fontSize: 17.0,
fontWeight: FontWeight.bold),
),
SizedBox(height: 5.0),
Text(
"12/12/19",
style: TextStyle(
color: Color(0xFF707070), fontSize: 17.0),
),
],
),
],
),
SizedBox(height: 60.0),
Row(
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"Max people (optional)",
style: TextStyle(
color: Color(0xFF707070),
fontSize: 17.0,
fontWeight: FontWeight.bold),
),
SizedBox(height: 5.0),
Text(
"5",
style: TextStyle(
color: Color(0xFF707070), fontSize: 17.0),
),
],
),
],
),
SizedBox(height: 20.0),
Row(
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"Max redemption per member (optional)",
style: TextStyle(
color: Color(0xFF707070),
fontSize: 17.0,
fontWeight: FontWeight.bold),
),
SizedBox(height: 5.0),
Text(
"5",
style: TextStyle(
color: Color(0xFF707070), fontSize: 17.0),
),
],
),
],
),
SizedBox(height: 20.0),
Row(
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"Number of redemption",
style: TextStyle(
color: Color(0xFF707070),
fontSize: 17.0,
fontWeight: FontWeight.bold),
),
SizedBox(height: 5.0),
Text(
"5",
style: TextStyle(
color: Color(0xFF707070), fontSize: 17.0),
),
],
),
],
),
SizedBox(height: 60.0),
Row(
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"Branches",
style: TextStyle(
color: Color(0xFF707070),
fontSize: 17.0,
fontWeight: FontWeight.bold),
),
SizedBox(height: 5.0),
Row(
children: <Widget>[
Checkbox(
value: allBranches,
onChanged: (bool value) {
setState(() {
allBranches = value;
});
},
),
Text(
"All Branches",
style: TextStyle(
color: Color(0xFF707070), fontSize: 17.0),
),
],
),
Row(
children: <Widget>[
Checkbox(
value: selectedBranches,
onChanged: (bool value) {
setState(() {
selectedBranches = value;
});
},
),
Text(
"Selected Branches",
style: TextStyle(
color: Color(0xFF707070), fontSize: 17.0),
),
],
),
],
),
],
),
SizedBox(height: 30.0),
Container(
width: 250.0,
child: FlatButton(
color: Colors.blue,
child: Text("Confirm", style: TextStyle(color: Colors.white)),
onPressed: () {
},
),
),
SizedBox(height: 30.0),
],
),
),
)
],
),
),
);
}
}
child: FlatButton(
color: Colors.blue,
child: Text("Confirm", style: TextStyle(color: Colors.white)),
onPressed: () {
setState(() async{
await db.collection("createdoffers").add(
{
'name': offerName,
'type': offerType,
'start': start,
'end': end,
}
);
}
);
}
)
child: FlatButton(
color: Colors.blue,
child: Text("Confirm", style: TextStyle(color: Colors.white)),
onPressed: () async {
_formKey.currentState.save();
await db.collection("createdoffers").add(
{
'name': offerName,
'type': offerType,
'start': start,
'end': end,
}
);
},
),