首页 > 解决方案 > Flutter中TextFormField中hintText的值是否可以保存?

问题描述

我需要将hintText 中的值保存到firebase。这是从上一页获取的值,我需要将其存储到数据库中。我可以存储用户输入的其他值,但不能存储提示文本。有什么办法可以保存吗?

这是我的小部件代码:

//This is what I cannot save it
Widget getFacilityName() => TextFormField(
   //enableInteractiveSelection: false,
   readOnly: true,
    decoration: InputDecoration(
      labelText: 'Hospital / Clinic Name',
      floatingLabelBehavior: FloatingLabelBehavior.always,
      hintText: widget.facilityName,
      border: OutlineInputBorder(),
      suffixIcon: Icon(Icons.local_hospital),
    ),
    // validator: (value) {
    //   if (value.length < 1) {
    //     return 'Please enter your name';
    //   } else {
    //     return null;
    //   }
    // },
    onSaved: (hintText) => setState(() => facName = hintText),
  );


  Widget buildName() => TextFormField(
    decoration: InputDecoration(
      labelText: 'Name',
      border: OutlineInputBorder(),
      suffixIcon: Icon(Icons.person),
    ),
    validator: (value) {
      if (value.length < 1) {
        return 'Please enter your name';
      } else {
        return null;
      }
    },
    onSaved: (value) => setState(() => name = value),
  );

  Widget buildIc() => TextFormField(
    inputFormatters: [maskFormatter1],
    decoration: InputDecoration(
      labelText: 'IC Number',
      border: OutlineInputBorder(),
      suffixIcon: Icon(Icons.credit_card),
    ),
    validator: (value) {
      if (value.length < 1) {
        return 'Please enter your IC number';
      } else {
        return null;
      }
    },
    onSaved: (value) => setState(() => ic = value),
  );

   Widget buildPhoneNo() => TextFormField(
    inputFormatters: [maskFormatter2],
    decoration: InputDecoration(
      labelText: 'Phone Number',
      border: OutlineInputBorder(),
      suffixIcon: Icon(Icons.local_phone),
    ),
    validator: (value) {
      if (value.isEmpty) {
        return 'Please enter your phone number';
      }
      else if (value.length < 13 || value.length > 14) {
        return 'Please enter a valid phone number';
      } else {
        return null;
      }
    },
    onSaved: (value) => setState(() => phoneNo = value),
  );

  Widget buildDate() => DateTimeFormField(
    decoration: InputDecoration(
      labelText: 'Select Date & Time',
      border: OutlineInputBorder(),
      suffixIcon: Icon(Icons.event_note),
    ),
    firstDate: DateTime(DateTime.now().year),
    lastDate: DateTime(DateTime.now().year + 5),
    validator: (value) {
      if (value == null) {
        return 'Please select a date';
      }
      else if(!value.isAfter(DateTime.now())){
        return 'Cannot book the date in the past';
      } else {
        return null;
      }
    },
    onSaved: (value) => setState(() => date = value),
  );

  Widget buildSubmit() => TextButton(
    child: Text('Confirm Appointment',
      style: TextStyle(
        fontSize: 18.0,
      ),),
    style: ButtonStyle(
      shape: MaterialStateProperty.all<RoundedRectangleBorder>(
        RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(28.0),
        )
      ),
      backgroundColor: MaterialStateProperty.all<Color>(Colors.blueGrey[300]),
      foregroundColor: MaterialStateProperty.all<Color>(Colors.white),
    ),
    onPressed: () {
      final isValid = formKey.currentState.validate(); //check the validator

      //to hide keyboard after click submit
      FocusScope.of(context).unfocus();

      if(isValid) {
        formKey.currentState.save();
        FirebaseFirestore.instance.runTransaction((Transaction transaction) async{
          CollectionReference reference = FirebaseFirestore.instance.collection('Appointment');
          await reference.add({"facilityName": "$facName", "name": "$name", "ic": "$ic", "phoneno": "$phoneNo", "datetime": "$date"});
        });

      }
    },
  );

在我的 Firestore 中,我得到这样的结果: FireStore 的屏幕截图

设施名称为空。

标签: firebaseflutterdartflutter-layouttextfield

解决方案


如果您从上一页获得hintText(的值),您应该直接在您的(因为它是只读的并且用户不会更改它!)中使用它,如下所示:widget.facilityNamebuildSubmit

FirebaseFirestore.instance.runTransaction((Transaction transaction) async{
          CollectionReference reference = FirebaseFirestore.instance.collection('Appointment');
          await reference.add({"facilityName": "${widget.facilityName}", "name": "$name", "ic": "$ic", "phoneno": "$phoneNo", "datetime": "$date"});
        });

推荐阅读