How to convert an object containing DateTime fields to JSON in Dart?

前端 未结 3 1051
借酒劲吻你
借酒劲吻你 2020-12-19 02:17

I try to convert an object to JSON.

  var obj = { \"dt\": new DateTime.now() };
  var s = stringify(obj);

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-19 02:57

    Zdeslav Vojkovic's answer is outdated now.

    The JSON.encode() method in dart:convert has an optional toEncodable method that is invoked for objects that are not natively serializable to JSON. It's then up to the user to provide a closure that returns an appropriate serialization of the DateTime.

提交回复
热议问题