Flutter app error - type 'Timestamp' is not a subtype of type 'DateTime'

后端 未结 14 1356
刺人心
刺人心 2020-12-16 12:49

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\'][\'         


        
相关标签:
14条回答
  • 2020-12-16 13:29

    add toDate() method .It will work

    DateTime dateTime = documents[i].data["duedate"].toDate();
    
    0 讨论(0)
  • 2020-12-16 13:29

    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']);
      }
    
    0 讨论(0)
提交回复
热议问题