我尝试使用共享首选项登录后存储用户数据,但我的代码给了我此错误消息:
The argument type 'Map<String, dynamic>' can't be assigned to the parameter type 'String'.
这是我的代码:
class UserPerfs
{
static Future<void> saveUserPerfs(User userData) async
{
SharedPreferences prefs = await SharedPreferences.getInstance();
String userJsonData = jsonDecode(userData.toJson());
await prefs.setString('currentUser', userJsonData);
}
}
我该如何解决这个问题?
您不能在
SharedPreferences中存储
Map
,它只接受单个变量,例如: String
,double
,int
或 boolean
,或 String
列表,所以解决方案是使用:Hive或Sqflite或任何其他支持Map
的软件包.