How to convert DateTime object to json

后端 未结 4 1268
忘掉有多难
忘掉有多难 2021-01-01 12:47

How to convert DateTime object to json? It throws Converting object to an encodable object failed., so is this a bug or it\'s just dart

4条回答
  •  春和景丽
    2021-01-01 13:34

    you are encoding object(DateTime) into other encodable object JSON.encode(DateTime.Now()) which is not possible in dart programming.

    So, convert it to dart supported Date to String conversion that is : add : .toIso8601String() at the end

    JSON.encode(DateTime.Now().toIso8601String()),this resolves your error. // i am taking DateTime.Now() just for example.

提交回复
热议问题