flutter - Flutter Json 格式异常
问题描述
在我的应用程序中,我试图从共享首选项中检索数据并将该值传递给提供者。我怎么会遇到异常
FormatException (FormatException: Unexpected character (at character 2)
{token: eyJh...
^
获取值并将值传递给提供者的函数如下
Future<void> insertUsertolocal() async {
final storage = new LocalStorage();
final _user = await storage.getValue(Constants.USER); // retrieves data from key
User user = User.fromJson(jsonDecode(_user)); // receives FortmatException
print(user.email);
}
json 数据以字符串形式存储在共享首选项中
_localStorage.setValue(Constants.USER, user.toString());
我该如何解决这个问题?
解决方案
您应该在 _localStorage.setValue 中使用 jsonEncode()。
推荐阅读
- typescript - 量角器:不移动到下一段代码
- mysql - 如何从多态数据透视表和 2 个目标中获得深层关系
- vb.net - 从自定义类列表中提取字符串列表
- python-3.x - 我已通过 Azure ADAL 进行身份验证,没有任何问题。接下来是什么?
- javascript - ES6 Symbol 是否保证 100% 的唯一标识符?
- php - 如何获得在 php 和 css 中工作的表格的宽度
- angular - 无法在 ASP 样板中添加语言 (ASPNetZero)
- javascript - 如何创建多过滤器库,如 woocommerce/电子商务产品过滤器
- python - 将 2D 列表分配给 2 个 Dataframe 列 Pandas
- react-native - 在活动指示器期间禁用用户交互。反应原生