问题描述
我已经在Flutter应用中实现了日期和时间选择器。我想将选择的日期和时间保存到Firebase Cloud Firestore数据库中
RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0)),elevation: 4.0,onpressed: () {
DatePicker.showDatePicker(context,theme: DatePickerTheme(
containerHeight: 210.0,),showTitleActions: true,minTime: DateTime(2010,1,1),maxTime: DateTime(2030,12,31),onConfirm: (date) {
print('confirm $date');
_date = '${date.year} - ${date.month} - ${date.day}';
setState(() {});
},currentTime: DateTime.Now(),locale: LocaleType.en);
},child: Container(
alignment: Alignment.center,height: 50.0,child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,children: <Widget>[
Row(
children: <Widget>[
Container(
child: Row(
children: <Widget>[
Icon(
Icons.date_range,size: 18.0,color: Colors.teal,Text(
" $_date",style: TextStyle(
color: Colors.teal,fontWeight: FontWeight.bold,fontSize: 18.0),],)
],Text(
" Change",style: TextStyle(
color: Colors.teal,color: Colors.white,SizedBox(
height: 20.0,RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0)),onpressed: () {
DatePicker.showTimePicker(context,onConfirm: (time) {
print('confirm $time');
_time = '${time.hour} : ${time.minute} : ${time.second}';
setState(() {});
},locale: LocaleType.en);
setState(() {});
},children: <Widget>[
Row(
children: <Widget>[
Container(
child: Row(
children: <Widget>[
Icon(
Icons.access_time,Text(
" $_time",RaisedButton(
color: Theme.of(context).primaryColor,textColor: Colors.white,child: Text(isEditMote ? "Update" : "Save"),onpressed: () async {
if (_key.currentState.validate()) {
try {
if (isEditMote) {
Note note = Note(
appID: _appIDController.text,doctorName: _doctorNameController.text,healthIssue: _healthIssueController.text,hospital: _hospitalController.text,id: widget.note.id,);
await FirestoreService().updateNote(note);
} else {
Note note = Note(
appID: _appIDController.text,);
await FirestoreService().addNote(note);
}
Navigator.pop(context);
} catch (e) {
print(e);
}
}
},);
} }
这是相关的示例代码。我向数据库传递了其他4个参数,就像我想传递此日期和时间选择器数据一样。前两个升高按钮包含日期和时间选择器,最后一个升高按钮涉及数据保存单元。
解决方法
您将日期时间另存为Epoch时间戳中的整数值或DateTime对象。
for i in [1,2,3]:
globals()['ax%s' % (i)] = i
OR
int selectedDT=pickedDateTime.millisecondsSinceEpoch;
Firestore.instance.collection('CollectionName').document(docName).setData({
'date_time': selectedDT
});