Home > Software design >  How to convert String data to offset data?
How to convert String data to offset data?

Time:06-13

I'm making a simple Draw app with Firebase. The app is uploading drawn lines (offset list, color, thickness, etc.) to Firestore. And when I turn on the app again, I try to retrieve the data from Firestore.

When uploading to Firestore, I couldn't upload the offset and color data as it is, so I uploaded it after converting it to String.

UploadDrawingModel uploadDrawingModel = UploadDrawingModel(
  pointList: currentLine!.pointList!.map((e) => e.toString()).toList(), // offset list
  colorString: currentLine!.color!.toString(), // "Color(0xff000000)"
  ....
);

And when I get the data from firestore, I have to return the data (offset, color) converted to String to the original type. Can I know how to convert String data to Offset and Color?

CodePudding user response:

I would prefer to create a pattern for the offsets list
for example, save them to firebase 13,5|25,3|... and when I retrieve them, call split('|') and map them back to offsets and for the color, you ca upload color.value this will return the integer value for it, red's value: 4294198070

UploadDrawingModel uploadDrawingModel = UploadDrawingModel(
      pointList: currentLine!.pointList!.map((e) => '${e.dx},${e.dy}').join('|'), // offset list
      colorString: currentLine!.color!.value.toString(), // "Color(0xff000000)"
      ....
  );

the above code will drop a record to your firebase as
pointsList: dx1,dy1|dx2,dy2|dx3,dy3|...
colorString: 0000000000

when you retrieve the records

myPointsList = record['pointsList'].toString().split('|').map((e) => Offset(
    num.parse(e.split(',').first).toDouble(),
    num.parse(e.split(',').last).toDouble(),
));
myColor = Color(int.parse(record['colorString']));
  • Related