来自 TextFormFields 的数据在 Firebase 数据库中为空

Datas from TextFormFields is null in Firebase database

我很难弄清楚这个问题(顺便说一句,我还是 flutter 的新手),我创建了一个带有表单的新屏幕,让用户填写信息,填写完后,有一个 validatoronSaved: EACH TextFormField() 目前,我只想将文本表单字段的数据保存到 Firebase 数据库中。

编辑: 我设法使用此代码使其以某种方式工作但是我输入的数据是 nulled 在 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,
                            }
                          );
                        },
                      ),

Ive also watched some tutorial but Im having trouble making it work since its kind of a bit different to what Im trying to do (I guess its a beginners problem, Im new to programming and I fell in love with flutter lol)

Now on my Firebase console, I created a new collection with some new dummy data just to fill in (mind you, I still dont save INPUTS from the app, just created a collection and put in some dummy data)

我的 firebase 图片如下:

NULLED 数据

我的代码在下面的屏幕表单中,我试图从 TextFormField 中的 INPUTS 中保存数据,并通过单击 FlatButton

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

My target for this is: 1. Save the data to firebase 2. Read that data and display it to a Container widget, I just want the C and R in CRUD for now

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

进一步阅读

您在 onsaved 期间传递了 textformfield 的值。但是你忘了保存表格。添加此 _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,
          }
        );
      },
    ),