简体   繁体   中英

Converting object to an encodable object failed: Instance of 'Offset'

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;
}

I would return the JSON explicitly:

return { 'point': {dx: "$point.dx", dy: "$point.dy"}, };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM