Dart: How do I convert an array of objects to an array of hashmaps? Dart: How do I convert an array of objects to an array of hashmaps? dart dart

Dart: How do I convert an array of objects to an array of hashmaps?


You can use the map and return the object that you want:

    List<Map<String, dynamic>> listOMaps = listOStuff            .map((something) => {                  "what": something.what,                  "the": something.the,                  "fiddle": something.fiddle,                })            .toList(); 


I'm not sure what exactly you're looking for, but there is a way to have custom objects encoded without having to specify it directly when you call the method.

What you have to do is implement a MethodCodec and/or MessageCodec that defines how your object is encoded and decoded. The easiest way is probably to subclass StandardMethodCodec and/or StandardMessageCodec (it might be enough to override StandardMessageCodec and pass it to StandardMessageCodec).

If you implement read & write correctly for your object, then all you have to do is pass the list of objects directly to your method call and flutter will handle the encoding.

Note that there are corresponding classes on the Android & iOS sides of things that you could use to have the data decoded directly to objects, and in fact you might have to implement them to get things to work depending on how you do it.