gpt4 book ai didi

flutter - 如何在dart中将List 插入Map 中?

转载 作者:行者123 更新时间:2023-12-03 03:01:16 25 4
gpt4 key购买 nike

我的问题是我有以下类(class)的 list :

class ingreso_Egreso_Dummy{
int tipo;
String monto;
String moneda;
String descripcion;
}

然后我想将数据插入到Map中,稍后将其转换为json并按以下方式创建:
    Map<String, dynamic> body;
body = {
"Cod_Prom": "01",
"CodCli": "003526",
"Status": _index_status.toString(),
"NOMBRE": controller_nombre.text,
"APELLIDOS": controller_apellidos.text,
"solicitud":[{
"Cod_Solicit": 1.toString(),
"Fecha": DateFormat("y-d-M").format(DateTime.now()),
"Status_Solicit": "E",}],
"prestamo":[{
"Monto_Solicit":controller_monto_solic.text,
"Plazo":controller_plazo.text,
"Cod_TipoPlazo":_index_tipoplazo.toString(),
"Nombre_Resp":controller_nombreresp.text,
"Telf_Resp":controller_telefonoresp.text,}],
"Ingresos": [{
//// here I want create a loop that returns a map for each value
//// of the list like this:

//// "Descripcion": Listaingresos[i].descripcion;

})
}]
};

非常感谢您的帮助。

最佳答案

// camelCaseStyle is a standard for class names for Dart     
class IngresoEgresoDummy {
int tipo;
String monto;
String moneda;
String descripcion;
Map<String, dynamic> toJson(){
return {
'tipo': tipo,
'monto': monto,
'monedo': moneda,
'descripcion': descripcion
};
}
}

在那之后
List<IngresoEgresoDummy> listaingresos= List();

Map<String, dynamic> body = {
// all your params
"Ingresos": listaingresos.map((ingreso) => ingreso.toJson()).toList()
// all your params
};

关于flutter - 如何在dart中将List <Class>插入Map <String,dynamic>中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58919964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com