是否有可能在颤振的TextFormField中保存hintText的值?



我需要将hintText中的值保存到firebase。这是从前一页采取的值,我需要将其存储到数据库中。我可以存储用户输入的另一个值但不能存储hintText。有办法挽救它吗?

这是我的小部件代码:
//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截图

设备名称为空。

如果你从上一页得到hintText(widget.facilityName的值),你应该直接在你的buildSubmit中使用它(因为它是只读的,用户不会改变它!),如下:

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

您要做的不是保存hintText的值,实际上您要保存来自前一页的变量facilityName的值到firebase。所以刚刚把facilityName的值发送给firebase。

widget.facilityName的值添加到facName之前的行

await reference.add({"facilityName": "$facName", "name": "$name", "ic": "$ic", "phoneno": "$phoneNo", "datetime": "$date"});

代码将是这样的:

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

,删除不必要的代码

最新更新