简体   繁体   中英

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. I have managed to save all the others except for this List

{
"roles":[

      "ROLE_USER",

      "ROLE_MODERATOR",

      "ROLE_ADMIN"

   ],
}

this is my 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

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.

Also after saving them how can I retrieve the individual role of a user like ROLE_USER from shared prefs?

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM