将日期和时间选择器数据保存到Flatter中的Firebase Cloud Firestore数据库中
我已经在我的flatter应用程序中实现了日期和时间选择器。我想将拾取的日期和时间保存到Firebase Cloud Firestore数据库中将日期和时间选择器数据保存到Flatter中的Firebase Cloud Firestore数据库中,firebase,flutter,datetime,google-cloud-firestore,save,Firebase,Flutter,Datetime,Google Cloud Firestore,Save,我已经在我的flatter应用程序中实现了日期和时间选择器。我想将拾取的日期和时间保存到Firebase Cloud Firestore数据库中 RaisedButton( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(5.0)), elevation: 4.0, onPressed: () {
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,
fontWeight: FontWeight.bold,
fontSize: 18.0),
),
],
),
),
color: Colors.white,
),
SizedBox(
height: 20.0,
),
RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0)),
elevation: 4.0,
onPressed: () {
DatePicker.showTimePicker(context,
theme: DatePickerTheme(
containerHeight: 210.0,
),
showTitleActions: true, onConfirm: (time) {
print('confirm $time');
_time = '${time.hour} : ${time.minute} : ${time.second}';
setState(() {});
}, currentTime: DateTime.now(), locale: LocaleType.en);
setState(() {});
},
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.access_time,
size: 18.0,
color: Colors.teal,
),
Text(
" $_time",
style: TextStyle(
color: Colors.teal,
fontWeight: FontWeight.bold,
fontSize: 18.0),
),
],
),
)
],
),
Text(
" Change",
style: TextStyle(
color: Colors.teal,
fontWeight: FontWeight.bold,
fontSize: 18.0),
),
],
),
),
color: Colors.white,
),
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,
doctorName: _doctorNameController.text,
healthIssue: _healthIssueController.text,
hospital: _hospitalController.text,
);
await FirestoreService().addNote(note);
}
Navigator.pop(context);
} catch (e) {
print(e);
}
}
},
),
],
),
),
),
);
RaisedButton(
形状:圆形矩形边框(
边界半径:边界半径。圆形(5.0)),
标高:4.0,
已按下:(){
DatePicker.showDatePicker(上下文,
主题:日期选取主题(
集装箱重量:210.0,
),
showttitleactions:对,
minTime:DateTime(2010,1,1),
maxTime:DateTime(2030,12,31),onConfirm:(日期){
打印(“确认$date”);
_日期=“${date.year}-${date.month}-${date.day}”;
setState((){});
},currentTime:DateTime.now(),locale:LocaleType.en);
},
子:容器(
对齐:对齐.center,
身高:50.0,
孩子:排(
mainAxisAlignment:mainAxisAlignment.spaceBetween,
儿童:[
划船(
儿童:[
容器(
孩子:排(
儿童:[
图标(
Icons.date\u范围,
尺寸:18.0,
颜色:Colors.teal,
),
正文(
“$\u日期”,
样式:TextStyle(
颜色:Colors.teal,
fontWeight:fontWeight.bold,
字体大小:18.0),
),
],
),
)
],
),
正文(
“改变”,
样式:TextStyle(
颜色:Colors.teal,
fontWeight:fontWeight.bold,
字体大小:18.0),
),
],
),
),
颜色:颜色,白色,
),
大小盒子(
身高:20.0,
),
升起的按钮(
形状:圆形矩形边框(
边界半径:边界半径。圆形(5.0)),
标高:4.0,
已按下:(){
DatePicker.showTimePicker(上下文,
主题:日期选取主题(
集装箱重量:210.0,
),
showtTitleActions:true,onConfirm:(时间){
打印(“确认$time”);
_时间='${time.hour}:${time.minute}:${time.second}';
setState((){});
},currentTime:DateTime.now(),locale:LocaleType.en);
setState((){});
},
子:容器(
对齐:对齐.center,
身高:50.0,
孩子:排(
mainAxisAlignment:mainAxisAlignment.spaceBetween,
儿童:[
划船(
儿童:[
容器(
孩子:排(
儿童:[
图标(
图标。访问时间,
尺寸:18.0,
颜色:Colors.teal,
),
正文(
“$次”,
样式:TextStyle(
颜色:Colors.teal,
fontWeight:fontWeight.bold,
字体大小:18.0),
),
],
),
)
],
),
正文(
“改变”,
样式:TextStyle(
颜色:Colors.teal,
fontWeight:fontWeight.bold,
字体大小:18.0),
),
],
),
),
颜色:颜色,白色,
),
升起的按钮(
颜色:主题。背景。原色,
textColor:Colors.white,
子项:文本(isEditMote?“更新”:“保存”),
onPressed:()异步{
if(_key.currentState.validate()){
试一试{
如果(isEditMote){
注(
appID:_appIDController.text,
doctorName:_doctorNameController.text,
healthIssue:_healthIssueController.text,
医院:_hospitalController.text,
id:widget.note.id,
);
等待FirestoreService().updateNote(注);
}否则{
注(
appID:_appIDController.text,
doctorName:_doctorNameController.text,
healthIssue:_healthIssueController.text,
医院:_hospitalController.text,
);
等待FirestoreService().addNote(注意);
}
Navigator.pop(上下文);
}捕获(e){
印刷品(e);
}
}
},
),
],
),
),
),
);
}
}
这是相关的示例代码。我向数据库传递了4个其他参数,就像我想要传递这个日期和时间选择器数据一样。前两个提升按钮包含日期和时间选择器,最后一个提升按钮
int selectedDT=pickedDateTime.millisecondsSinceEpoch;
Firestore.instance.collection('CollectionName').document(docName).setData({
'date_time': selectedDT
});
DateTime selectedDT=pickedDateTime;
Firestore.instance.collection('CollectionName').document(docName).setData({
'date_time': selectedDT
});