I am using mongo_dart.
Some field contains the Int64
type.
I tried to convert the Map<String, dynamic>
type that receive from MongoDB to the JSON
by using the jsonEncode
function but I got this error:
Converting object to an encodable object failed: Instance of 'Int64'
How to fix this?
Should I change the field type to String
?
Should I create a function that converts every element in the Map<String, dynamic>
from Int64
to Int
?
JSON is based on JavaScript, and JavaScript numbers are IEEE-754 double-precision floating-point numbers (double
s), which have a 53-bit significand. They cannot store all 64-bit integers without loss of precision. (JSON implementations can vary in how numbers are stored, but if you care about interoperability, then you should assume double
s as the lowest common denominator.)
Therefore, if you use the full range of 64-bit integers, you're better off storing 64-bit integers as strings.
If 53-bits are sufficient for you, or if you don't care about interoperability and your JSON values will be read and written only by Dart VM code (not Dart for the Web), then you can use int
s everywhere and not bother with Int64
.