我通过Custom Paint制作了一个简单的绘图函数。当我画一条线时,这条线的坐标被保存为偏移。我要把这个偏移列表上传到Firestore。
但是,Offset不能上传到Firestore
Invalid argument: Instance of 'Offset'
我要上传的Json数据如下
I/flutter ( 6636): │ 🐛 {
I/flutter ( 6636): │ 🐛 "nickname": "dgh",
I/flutter ( 6636): │ 🐛 "point": [
I/flutter ( 6636): │ 🐛 "Offset(110.0, 265.4)",
I/flutter ( 6636): │ 🐛 "Offset(110.5, 265.6)",
I/flutter ( 6636): │ 🐛 "Offset(110.9, 266.1)",
I/flutter ( 6636): │ 🐛 "Offset(111.3, 266.7)",
I/flutter ( 6636): │ 🐛 ],
I/flutter ( 6636): │ 🐛 "color": "MaterialColor(primary value: Color(0xffff5722))",
I/flutter ( 6636): │ 🐛 "width": 5.0
I/flutter ( 6636): │ 🐛 }
// firebase upload code
await FirebaseFirestore.instance
.collection(COL_ROOMS)
.doc(widget.roomKey)
.collection(COL_DRAW)
.doc()
// .set(currentLine!.toJson())
// test
.set({
"name": "test",
"offset": Offset(1, 1), // error!
})
.then((value) => logger.d("upload!"))
.catchError((error) => logger.w(error));
如何上传偏移量数据到firestore?
您不能将dart对象推送到firebase,您需要将它们转换为Map,这将更有用,我建议您执行以下操作
await FirebaseFirestore.instance
.collection(COL_ROOMS)
.doc(widget.roomKey)
.collection(COL_DRAW)
.doc()
// .set(currentLine!.toJson())
// test
.set({
"name": "test",
"offset": {
"dx": offset.dx,
"dy": offset.dy,
}, // error!
})
.then((value) => logger.d("upload!"))
.catchError((error) => logger.w(error));
当您从firebase检索它时,
Offset(docSnapshot[‘dx’], docSnapshot[‘dy’]);