Reputation: 113
I'm trying to send Dart Offset points by encoding it to Json format using 'dart:convert' library. I have gone through the documentation https://api.flutter.dev/flutter/dart-convert/jsonEncode.html.
The error I'm getting is for serializing the inbuilt classes.
The following JsonUnsupportedObjectError was thrown while handling a gesture:
Converting object to an encodable object failed: Instance of 'Offset'
How can i serialize inbuilt class like Offset and Paint class, is this the correct way to send the data to server?
TestData class contains Offset point and toJson() function
class TestData {
TestData(this.point);
Offset point;
toJson() {
return{
'point': point,
};
}
}
Encoder function
String jsonEncoder() {
Map testDataMap = this.testDataObj.toJson();
String jsonStringData = jsonEncode(testDataMap);
return jsonStringData;
}
Upvotes: 2
Views: 9544
Reputation: 2367
I would return the JSON explicitly:
return { 'point': {dx: "$point.dx", dy: "$point.dy"}, };
Upvotes: 1