Joh*_*Boy 2 object hashmap dart flutter
我想将对象转换为哈希图,以便可以使用Flutter方法通道将数据发送到Android。
我已经考虑过逐一迭代并映射它们,但是必须有一种更优雅的方法来实现此目的...
例:
宾语
class Something {
Something(this.what, this.the, this.fiddle);
final String what;
final int the;
final bool fiddle;
}
Run Code Online (Sandbox Code Playgroud)
别的地方
List<Something> listOStuff = List<Something>.generate(10, (int index){
return Something(index.toString(), index, false,);
});
List<Map<String, dynamic>> parseToMaps(List<Something> listOStuff){
List<Map<String, dynamic>> results;
// do something crazy to get listOStuff into Map of primitive values for each object
// preferably a built in method of some sort... otherwise, i guess i'll just iterate...
// maybe even an imported package if such thing exists
return results;
}
List<Map<String, dynamic>> listOMaps = parseToMaps(listOStuff);
Run Code Online (Sandbox Code Playgroud)
您可以使用map并返回所需的对象:
List<Map<String, dynamic>> listOMaps = listOStuff
.map((something) => {
"what": something.what,
"the": something.the,
"fiddle": something.fiddle,
})
.toList();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2718 次 |
| 最近记录: |