Firebase数据库中来自TextFormFields的数据为null

时间:2019-12-19 08:52:19

标签: android flutter flutter-layout flutter-dependencies mobile-development

我很难弄清楚这个问题(我还是扑朔迷离),我创建了一个新屏幕,其中的表单可以让用户用信息填充信息,并在填写信息后显示{{1 }}和validator分别位于每个 onSaved:上,我只希望文本格式字段将数据保存到Firebase数据库。

编辑:我设法使用此代码使其工作,但Ive输入的数据是Firebase数据库中的TextFormField()(第二张图片):

nulled
  

我也看过一些教程,但是Im与它试图做的有点不同(我想这是一个初学者的问题,Im是编程的新手,我爱上了扑朔迷离的笑声)<< / p>      

现在,在Firebase控制台上,我创建了一个新集合,其中包含一些新内容   虚拟数据只是为了填写(请注意,我仍然不保存来自   该应用程序,刚刚创建了一个集合并放入了一些虚拟数据)

我的火力基地的图片如下:

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, } ); }, ), 数据 enter image description here

我的代码在下面的屏幕表格中,我试图将输入数据保存在TextFormField中,然后通过单击NULLED

将其全部保存到我的数据库中
  

我的目标是: 1。将数据保存到firebase2。读取该数据并将其显示在Container小部件中,我现在只希望CRUD中的C和R

FlatButton

我尝试过的很

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),
                  ],
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}

2 个答案:

答案 0 :(得分:1)

出了什么问题

child: FlatButton(
                        color: Colors.blue,
                        child: Text("Confirm", style: TextStyle(color: Colors.white)),
                        onPressed: () async {
                          await db.collection("createdoffers").add(
                            {
                              'name': offerName, // This is null, try to change the way you save data via setState
                              'type': offerType,
                              'start': start,
                              'end': end,
                            }
                          );
                        },
                      ),

您可以做什么

如果您继续执行此操作,请尝试尝试TextEditingController

// Declaration
TextEditingController _offerNameTextController = TextEditingController();

// Usage
TextFormField(
   controller: _offerNameTextController,
   ...
)

// Retrieving data from the input field
FlatButton(
   onPressed: () {
      // Utilize the value (eg. on your Firebase saving method
     print(_offerNameTextController.text);
   }
)

进一步阅读

答案 1 :(得分:0)

您正在textformfield期间传递onsaved的值。但是您忘了保存表格。添加此_formKey.currentState.save();

    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,
          }
        );
      },
    ),