firebase - 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 的屏幕截图
设施名称为空。
解决方案
如果您从上一页获得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"});
});
推荐阅读
- bit-manipulation - 如何简化这个“一次清除多个位”功能?
- php - WordPress子、孙、曾孙类别必须使用父类别模板
- java - ANDROID_SDK_ROOT=undefined(推荐设置)JDK 8 需求检查失败
- python - Python:对列表进行排序,使 5.10 在 5.9 之后
- java - Gradle:无法通知构建侦听器
- botframework - 我可以合并现有的 Bot 构建以在 Bot Composer 中运行吗
- git - Git 在单次提交中从 master 获取快照
- javascript - 如何将页面链接导航到特定 div?
- java - 无法识别的 VM 选项“AggressiveOpts”
- google-chrome - 安装时有什么方法可以自动固定 Chrome 扩展程序?