如何将此类转换为JSON或List?
class cliente {
int id;
String nome;
String apelido;
String sexo;
String status;
}
Run Code Online (Sandbox Code Playgroud)
编辑
我改变了我的课程,对我的情况很好:
class client {
Map<String, dynamic> fields => {
"id": "",
"name": "",
"nickname": "",
"sex": "",
"status": "",
}
Run Code Online (Sandbox Code Playgroud)
然后我用:
client.fields["id"] = 1;
client.fields["name"] = "matheus";
sqlite.rowInsert("insert into client(id, name)", client.fields.Keys.toList(), client.fields.Values.toList());
Run Code Online (Sandbox Code Playgroud)
只需在类中创建一个方法并返回一个 Map<String, dynamic>
class cliente {
int id;
String nome;
String apelido;
String sexo;
String status;
Map<String, dynamic> toJson() => {
'id': id,
'nome': nome,
'apelido': apelido,
'sexo': sexo,
'status': status,
};
}
Run Code Online (Sandbox Code Playgroud)
并使用它例如:
final dataObject = new client();
...fill your object
final jsonData = dataObject.toJson();
Run Code Online (Sandbox Code Playgroud)
您也可以尝试使用此包来避免编写所有字段:https://pub.dartlang.org/packages/json_serializable
| 归档时间: |
|
| 查看次数: |
493 次 |
| 最近记录: |