I'm writing a Flutter Plugin that sends a List of Maps (List<Map<String, double>>
) from the Platform specific side. On the Platform specific side, I'm sending these Objects using the Default Message Codec.
// (example: Android side)
List<Map<String, Double>> out = new ArrayList<>();
... fill the map ...
result.success(out);
I'm receiving these values as follows on the Dart side:
static Future<List<Map<String, double>>> getListOfMaps() async {
var traces = await _channel.invokeMethod('getListOfMaps');
print(traces); // works
return traces;
}
Printing the values gives the correct values. However, on the Function Return, I'm getting the following Error type 'List<dynamic>' is not a subtype of type 'FutureOr<List<Map<String, double>>>'
on run-time, indicating that the cast from the dynamic value to the specific Map<String, double>
didn't work.
How do I cast nested values coming from MethodChannels correctly in Dart?
As pointed out in the comments, I have to cast every value with unknown runtime type individually to the expected type.
static Future<List<Map<String, double>>> getListOfMaps() async {
List<dynamic> traces = await _channel.invokeMethod(...);
return traces
.cast<Map<dynamic, dynamic>>()
.map((trace) => trace.cast<String, double>())
.toList();
}
You can now use invokeListMethod
:
Since invokeMethod can only return dynamic maps, we instead create a new typed list using List.cast.
var channel = MethodChannel('foo_channel');
var list = await channel.invokeListMethod<Map<String, double>>('methodInJava');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With