Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 211 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flutter app error - type 'Timestamp' is not a subtype of type 'DateTime'

#1
I'm fetching data cloud firestore & trying to show in my app by using the following piece of code.

new Text(timeago.format(document.data['tripDoc']['docCreatedOn'])),

I'm using ```timeago``` dart package to format that. But, after making updating to latest cloud firestore plugin I'm getting this error -

Another exception was thrown: type 'Timestamp' is not a subtype of type 'DateTime'

Can't able to understand how to parse this 'TimeStamp' object to 'DateTime'. Because ```timeago``` plugin need data in DateTime object format.
Reply

#2
DateTime.fromMillisecondsSinceEpoch(timeStamp);
DateTime.fromMicrosecondsSinceEpoch(timeStamp);
Reply

#3
var date = DateTime.fromMillisecondsSinceEpoch(timestamp)
Reply

#4
```.toDate()``` worked for me. Now the modified code is -

new Text(timeago.format(document.data['tripDoc']['docCreatedOn'].toDate()))

Hope, it'll help someone.
Reply

#5
I think this is more reliable

DateTime updateDateTime = DateTime.fromMillisecondsSinceEpoch(
map['updatedatetime'].millisecondsSinceEpoch);

Reply

#6
For some funny reason, I can't use toDate() on Android. Have to use it for iOS. So I'm forced to use a platform check like this:

Theme.of(context).platform == TargetPlatform.iOS
? DateFormat('dd MMM kk:mm').format(document['timestamp'].toDate())
: DateFormat('dd MMM kk:mm').format(document['timestamp'])

Reply

#7
You can try this..

timeago.format(DateTime.tryParse(timestamp))
like in yours it will be

timeago.format(DateTime.tryParse(document.data['tripDoc']['docCreatedOn']))
Reply

#8
Firestore is returning a Timestamp object, which consists of seconds and nanoseconds. Oddly, on iOS you can indeed just use a .toDate() and it works. But that breaks on Android as toDate() is not a method. So you can do a platform check if you want, but the universal solution is to use Firestore's Timestamp:

import 'package:cloud_firestore/cloud_firestore.dart';

DateTime _convertStamp(Timestamp _stamp) {

if (_stamp != null) {

return Timestamp(_stamp.seconds, _stamp.nanoseconds).toDate();

/*
if (Platform.isIOS) {
return _stamp.toDate();
} else {
return Timestamp(_stamp.seconds, _stamp.nanoseconds).toDate();
}
*/

} else {
return null;
}
}

and then pass your model to it:

SomeModel.fromJson(Map<String, dynamic> parsedJson) {
updatedAt = _convertStamp(parsedJson['updatedAt']);
}


Reply

#9
Ios and Android will not receive same type. Ios receive the timestamps as TimeStamp and Android receive it as DateTime already. So for fixing this issue I just created this little function. This will return a DateTime and let use format it etc.

import 'dart:io';

import 'package:cloud_firestore/cloud_firestore.dart';

DateTime parseTime(dynamic date) {
return Platform.isIOS ? (date as Timestamp).toDate() : (date as DateTime);
}

Reply

#10
add toDate() method .It will work



DateTime dateTime = documents[i].data["duedate"].toDate();
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through