dart_mappable icon indicating copy to clipboard operation
dart_mappable copied to clipboard

DateTime doesn't work as expected

Open lukepighetti opened this issue 1 year ago • 5 comments

There are many issues on this already but I'll just illustrate it quite simply. The way dart_mappable handles dates out-of-the-box is unlike anything I've ever seen in a serialization package. Quite simply, any model with a DateTime cannot pass equality checks after it's been serialized and deserialized.

@MappableClass()
class DateTimeExample with DateTimeExampleMappable {
  final DateTime timestamp;

  DateTimeExample({required this.timestamp});
}


void main() async {
  final x = DateTimeExample(timestamp: DateTime(2020));
  final x2 = DateTimeExampleMapper.fromJson(x.toJson());
  print("this should be true, it's ${x == x2}"); // "false"
}

lukepighetti avatar Nov 14 '24 15:11 lukepighetti

There are some decisions I would do otherwise now, but I simply don't want to make a breaking change now.

DateTime encoding is one of them.

When I make a new major version I can change it.

schultek avatar Nov 14 '24 16:11 schultek

Amazing package but this issue deserves more attention since it is very fundamental—it’d be great to get an update. Thanks! 🙏

devberkay avatar Dec 18 '24 11:12 devberkay

Is there a temporary solution to fix this?

cheymos avatar Jan 13 '25 11:01 cheymos

As a workaround, you can put DateTimeMapper.encodingMode = DateTimeEncoding.iso8601String in your main().

schultek avatar Jan 13 '25 19:01 schultek

As a workaround, you can put DateTimeMapper.encodingMode = DateTimeEncoding.iso8601String in your main().

Thank you!

cheymos avatar Jan 23 '25 18:01 cheymos