dart 如何访问map中示例的值?

kpbpu008  于 2023-03-15  发布在  其他
关注(0)|答案(2)|浏览(145)

我尝试使用一些共享首选项方法,如setStringListgetStringList,但在使用get和显示其值时 我在控制台上看到下面这行代码:

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

下面是代码:

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'])}
sdnqo3pr

sdnqo3pr1#

你可以共享SearchDelegateModel,或者你可以像这样访问你的数据模型。
print("${getpre[0].nameVariableInSearchDelegateModel}");

vqlkdk9b

vqlkdk9b2#

通过查看代码,您可以在打印行中使用它

getPre.forEach((e) => print(e.name));

相关问题