Convert flutter Datetime into Firebase Timestamp

6,580 views
Skip to first unread message

flutter testing

unread,
Sep 14, 2019, 2:59:43 AM9/14/19
to Flutter Development (flutter-dev)
Does anyone has any suggestion to convert flutter Datetime into Firebase Timestamp so it can be stored into firebase database as firebase does not have any Datetime field?

Timestamp _dueDate = onValue as Timestamp // this syntax to convert Datetime into Timestamp gives an error. Error is with or without cast.


Error with cast:

E/flutter ( 9348): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: type 'DateTime' is not a subtype of type 'Timestamp' in type cast


Here's the code:
//.......................................................

Widget inputDueDate() {
return Padding(padding: new EdgeInsets.only(left: 10.0, right: 10.0),
child: TextField(
onTap: ()=> ShowDateCalenddar(context, ).then((onValue){
Timestamp _dueDate = onValue as Timestamp; // Convert DateTime into timestamp so it can be stored into firebase document
}),

style: Theme.of(context).textTheme.body2,
controller: _dueDateController,
decoration: InputDecoration(labelText: homeworkConfig.dueDateLabel,
enabled: (widget.CRUDFlag == 'READ' ? false: true),
),
maxLines: 1,
),
);
}


Future<DateTime> ShowDateCalenddar(BuildContext context, ) async {
final DateTime picked = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2019, 1),
lastDate: DateTime(2101));
if (picked != null) {
setState(() {
_dueDateController = TextEditingController(text: picked.toString());
});
}
return picked;
}

Jagannathan Srinivasan

unread,
Sep 14, 2019, 5:55:45 AM9/14/19
to Flutter Development (flutter-dev), flutter testing
I store timestamp in firebase as milliseconds since epoch. It works very well for me


From: flutt...@googlegroups.com <flutt...@googlegroups.com> on behalf of flutter testing <fluttera...@gmail.com>
Sent: Saturday, September 14, 2019 12:29:43 PM
To: Flutter Development (flutter-dev) <flutt...@googlegroups.com>
Subject: Convert flutter Datetime into Firebase Timestamp
 
--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/9413a322-7c64-437d-80c9-687f86fdcecd%40googlegroups.com.

flutter testing

unread,
Sep 14, 2019, 12:17:48 PM9/14/19
to Flutter Development (flutter-dev)
Thanks. Can you pl share your code? Converting from Datetime to millisecondsSinceEpoch, and then converting it back to Datetime is changing the value of the data. 

onTap: () => ShowDateCalenddar(
context,
).then((onValue) {
printText('1. onValue', onValue.toString());
var _dueDate = onValue.millisecondsSinceEpoch; // Convert DateTime into timestamp so it can be stored into firebase document
printText('2. onValue.millisecondsSinceEpoch', _dueDate.toString());
DateTime _dueDate2 = DateTime.fromMicrosecondsSinceEpoch(_dueDate);
printText('3. DateTime.fromMicrosecondsSinceEpoch(_dueDate)', '$_dueDate2');

}),

I/flutter (12387): onValue : 2019-09-14 12:14:19.119163
I/flutter (12387): onValue.millisecondsSinceEpoch : 1568477659119
I/flutter (12387): DateTime.fromMicrosecondsSinceEpoch(_dueDate) : 1970-01-18 22:41:17.659119
To unsubscribe from this group and stop receiving emails from it, send an email to flutt...@googlegroups.com.

Jagannathan Srinivasan

unread,
Sep 14, 2019, 12:51:37 PM9/14/19
to flutter testing, Flutter Development (flutter-dev)
You seem to have a small typo...you're using millisecondsSinceEpoch in one conversion and microssecondsSinceEpoch in the reverse :-)

To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/8d2de50b-d6f4-43da-9106-81f9ad21b79d%40googlegroups.com.

Muhammad Kashif

unread,
May 25, 2020, 3:17:39 PM5/25/20
to Flutter Development (flutter-dev)
You can convert flutter DateTime into firebase Timestamp by using function Timestamp.fromMillisecondsSinceEpoch()

Timestamp.fromMillisecondsSinceEpoch(DateTime.now().millisecondsSinceEpoch).toString()
Reply all
Reply to author
Forward
0 new messages