【问题标题】:is it possible to store DocumentReference in shared_preferences in flutter?是否可以在颤动中将 DocumentReference 存储在 shared_preferences 中?
【发布时间】:2021-02-07 04:44:03
【问题描述】:

当我尝试存储 DocumentReference 类型时,出现以下错误

未处理的异常:将对象转换为可编码对象失败:“DocumentReference”实例

我在想解析错误,但我不知道确切

【问题讨论】:

  • 请分享您遇到异常的几段代码和完整的堆栈跟踪
  • JSON OBJECT ------> {subTotal: 14999.0, grandTotal: 14999.0, deliveryCharge: 0.0, deliveryLocation: null, products: [{quantity: 1, productRef: DocumentReference(products/5RVVNBMXxiYqJ0lTrfYX) }], userRef: DocumentReference(users/2rdRXf2on6h0B3igNvwn0IIOWox1), createdAt: 2020-10-24 21:15:25.083351, paymentType: null, tax: 0.0} 存储到sharedPreference方法------------> static Future setCartInfo(Map cartInfo) async { SharedPreferences prefs = await SharedPreferences.getInstance();返回 prefs.setString('cartInfo', jsonEncode(cartInfo)); }
  • 下面是错误 Unhandled Exception: Converting object to an encodable object failed: Instance of 'DocumentReference'

标签: flutter dart


【解决方案1】:

文档参考是指从文档参考中获得的数据吗?

共享首选项中的数据存储在键值对中。该值不能是对象,它必须是 intstringbool 等。因此,您首先需要将存储在类中的值转换为 json 字符串。另请注意,并非所有对象都可以转换为 json。对于您的特定错误,请查看: 看:Converting object to an encodable object failed

话虽如此,您可以做的事情是首先从文档引用中获取数据,然后将其存储在shared_pref 上,而不是尝试存储 document_reference 本身。

您还可以获取文档的 id,然后将该特定文档路径存储为键,将数据存储为值,以防您需要访问该特定文档。

【讨论】:

  • 我需要存储DocumentReference类型的数据
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-25
  • 2015-05-30
  • 2016-10-21
相关资源
最近更新 更多