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

后端 未结 14 1403
刺人心
刺人心 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:13

    If you use JsonSerializable, Use JsonConverter

    class TimestampConverter implements JsonConverter {
      const TimestampConverter();
    
      @override
      DateTime fromJson(Timestamp timestamp) {
        return timestamp.toDate();
      }
    
      @override
      Timestamp toJson(DateTime date) => Timestamp.fromDate(date);
    }
    
    @JsonSerializable()
    class User{
      final String id;
      @TimestampConverter()
      final DateTime timeCreated;
    
      User([this.id, this.timeCreated]);
    
      factory User.fromSnapshot(DocumentSnapshot documentSnapshot) =>
          _$UserFromJson(
              documentSnapshot.data..["_id"] = documentSnapshot.documentID);
    
      Map toJson() => _$UserToJson(this)..remove("_id");
    }
    

提交回复
热议问题