简体   繁体   English

Flutter:未处理的异常:键入“列表”<dynamic> ' 不是类型 'List 的子类型<string> '</string></dynamic>

[英]Flutter:Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<String>'

I have a json string that I am trying to save to Shared prefs.我有一个 json 字符串,我试图将其保存到共享首选项。 I have managed to save all the others except for this List我已经设法保存了除此列表之外的所有其他人

{
"roles":[

      "ROLE_USER",

      "ROLE_MODERATOR",

      "ROLE_ADMIN"

   ],
}

this is my json这是我的 json

 {

   "id":44,

   "username":"user@gmail.com",

   "email":"user@gmail.gom",

   "roles":[

      "ROLE_USER",

      "ROLE_MODERATOR",

      "ROLE_ADMIN"

   ],

   "userid":"439cea61-602b-4b1f-a32b-41487775ba00",

   "surname":"user",

   "firstname":"user1",

   "telephoneno":"2113456",

   "whatsappno":"2113456",

   "active":1,

   "studyrole":"TRAINEE",

   "tokenType":"Bearer",

   "accessToken":"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJybXVtb0BtZ2ljLnVtYXJ5bGFuZC5lZHUiLCJpYXQiOjE2MTEwNzk0NDcsImV4cCI6MTYxMTE2NTg0N30.EFX48OAD2MRtg3jCBvvH-Sna4jG8P5FX_LCbyBw38-UX4M85y6l15ISxlF02qK7rhzddiN9KZ8IIvLxhd0mZUA"

}

what I have managed so far到目前为止我所管理的

final http.Response response = await http.post(
    url,
    headers: <String, String>{
      'Content-Type': 'application/json; charset=UTF-8',
    },
    body: jsonEncode(<String, String>{
      'username': username,
      'password': password,
      'appversion': appversion,
      'latitude': latitude,
      'longitude': longitude,
      'imeI': imeI,
    }),
  );
  print(response.body);
  SharedPreferences prefs = await SharedPreferences.getInstance();
  var parse = jsonDecode(response.body);

    await prefs.setString('username', parse["username"]);
      await prefs.setString('accessToken', parse["accessToken"]);
      await prefs.setString('surname', parse["surname"]);
      await prefs.setString('firstname', parse["firstname"]);
      await prefs.setInt('id', parse["id"]);
      await prefs.setBool('active', parse["active"] == 1);
      await prefs.setString('userid', parse["userid"]);

How I am trying to save the list in shared preference我如何尝试将列表保存在共享首选项中

 await prefs.setStringList('roles', parse['roles']);

Whenever I try to save the json List I end up with an error每当我尝试保存 json 列表时,我都会遇到错误

Unhandled Exception: type 'List' is not a subtype of type 'List'未处理的异常:“列表”类型不是“列表”类型的子类型

How can I extract the roles and save them in Shared preference as individual items in flutter.如何提取角色并将它们作为 flutter 中的单个项目保存在共享首选项中。

Also after saving them how can I retrieve the individual role of a user like ROLE_USER from shared prefs?同样在保存它们之后,我如何从共享首选项中检索像 ROLE_USER 这样的用户的个人角色?

Probably you are missing to cast list, try this one可能你错过了演员表,试试这个

await prefs.setStringList('roles', parse['roles'].cast<String>());

Based on your second question (searching users where by a field)根据您的第二个问题(按字段搜索用户)

Doing this is a bit tricky because shared preferences is a key/value storage, not relational database.这样做有点棘手,因为共享首选项是键/值存储,而不是关系数据库。 i strongly recommend to use a database, otherwise you'll face too many complexity.我强烈建议使用数据库,否则您将面临太多复杂性。

Why?为什么? You want to store list of users(i'm assuming it's User object) in shared preferences, but the code scope you shared is for single user, list of users is required indeed.您想在共享首选项中存储用户列表(我假设它是用户对象),但是您共享的代码 scope 是针对单个用户的,确实需要用户列表。 And to store a list, it needs to be encoded to json string, and to retrieve as list, need to be decoded from json string.而要存储一个列表,它需要编码为 json 字符串,而要检索为列表,需要从 json 字符串中解码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter '未处理的异常:类型'列表<dynamic> ' 不是类型 'List 的子类型<class> '</class></dynamic> - Flutter 'Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<class>' 未处理的异常:键入“列表”<dynamic> &#39; 不是 &#39;List 类型的子类型<String> &#39; - Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<String>' 未处理的异常:键入“列表”<dynamic> ' 不是类型 'List 的子类型<string?> '</string?></dynamic> - Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<String?>' 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是类型 'List 的子类型<dynamic> ' 在请求中的类型转换 flutter</dynamic></string,> - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>' in type cast in request flutter 未处理的异常:类型“String”不是类型“List”的子类型<dynamic> ' 在类型转换中</dynamic> - Unhandled Exception: type 'String' is not a subtype of type 'List<dynamic>' in type cast Dart 未处理的异常:键入“列表”<dynamic> ' 不是类型 'Family' 的子类型,- Flutter</dynamic> - Dart Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Family', - Flutter 未处理的异常:键入“列表”<dynamic> ' 不是 'SubJsonModel' 类型的子类型 - flutter</dynamic> - Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'SubJsonModel' - flutter 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是类型 'List 的子类型<dynamic> '</dynamic></string,> - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>' 未处理的异常:键入“列表”<dynamic> &#39; 不是类型 &#39;Map 的子类型<String, dynamic> &#39; - Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' 未处理的异常:键入“列表<dynamic> ' 不是 'Map 类型的子类型<string, dynamic></string,></dynamic> - Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM