I have a list contacts
that stores Contact
which is a type acquired from contact_services
package
List<Contact> contacts = [];
and I store the List using SharedPrefrences
by first jsonEncode
it and then save it
void saveContacts() async{
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString('list', jsonEncode(contacts));
}
but when I try to load the list it returns an exception type 'List<dynamic>' is not a subtype of type 'List<Contact>'
void loadList() async{
SharedPreferences prefs = await SharedPreferences.getInstance();
contacts = await jsonDecode(prefs.getString('list'));
}
Updated code to highlight changes :
this is the entire saveContacts
function :
void saveContacts() async{
SharedPreferences prefs = await SharedPreferences.getInstance();
var json = jsonEncode(contacts, toEncodable: (e) => e.toMap());
await prefs.setString('list', jsonEncode(json));
}
but I am receiving error : The method 'toMap' isn't defined for the type 'Object'.
contacts is just a List that stores Contact
type
List<Contact> contact;
originally contact
was stored in a separate folder (global) in order to be easily accessible, but this doesn't affect the outcome of the jsonEncode