skip to Main Content

I am trying to use some shared preferences methods such as setStringList and getStringList but when using the get and displaying its values ​​I get the following line on the console:

{0: Instance of 'SearchDelegateModel', 1: Instance of 'SearchDelegateModel'}

Here is the code:

IconButton(
  icon: Icon(Icons.search),
  onPressed: () async {
    SharedPreferences prefs = await SharedPreferences.getInstance();

    final busqueda = await showSearch(context: context, delegate: SearchDelegateMenuPrincipal("Buscar...", this.historialMenuPrincipal));
    if (busqueda != null) {
      historialMenuPrincipal.removeWhere((item) => item.apodo == busqueda.apodo);
      this.historialMenuPrincipal.insert(0, busqueda);
      final historialbusquedacastead = historialMenuPrincipal.map((e) => e.toString()).toList();
      prefs.setStringList("HistorialBusquedaPreferences", historialbusquedacastead);
      var getpre = prefs.getStringList("HistorialBusquedaPreferences");
      print(getpre.asMap()); // the print shows {0: Instance of 'SearchDelegateModel', 1: Instance of 'SearchDelegateModel'}
    }
  },
),
//////////
List MenuPrincipalViewproductFromJson(String str) => List<SearchDelegateModel>.from(json.decode(str).map((x) => SearchDelegateModel.fromJson(x)));
 SearchDelegateModel{
 String apodo;
 String name;
SearchDelegateModel({this.apodo,this.name})
}
factory SearchDelegateModel.fromJson(Map<String, dynamic> parsedJson){
    return SearchDelegateModel(
name: parsedJson['name'],
        apodo : parsedJson['apodo'])}

2

Answers


  1. Can you share SearchDelegateModel, or you can access you data model like this.

    print("${getpre[0].nameVariableInSearchDelegateModel}");

    Login or Signup to reply.
  2. By looking at your code, you could use this in your print line instead

    getPre.forEach((e) => print(e.name));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search