I have implemented the Date and Time picker in my Flutter app. I want to save picked Date and Time into Firebase Cloud Firestore Database
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);
}
}
},
),
],
),
),
),
);
} }
This is the relevant sample code. I passed 4 other parameters to database, like that I want to pass this Date and Time picker data. First two Raised Buttons contains the Date and Time picker, final raised Button regarding the Data Saving unit.
Solution 1: Gursewak Singh
You saved date time as integer value in Epoch time stamp or as a DateTime object.
int selectedDT=pickedDateTime.millisecondsSinceEpoch;
Firestore.instance.collection('CollectionName').document(docName).setData({
'date_time': selectedDT
});
OR
DateTime selectedDT=pickedDateTime;
Firestore.instance.collection('CollectionName').document(docName).setData({
'date_time': selectedDT
});