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\'][\'
add toDate() method .It will work
DateTime dateTime = documents[i].data["duedate"].toDate();
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']);
}