Ok, I followed completely this example: https://flutter.io/platform-channels/
and tried to modify it like so:
List<double> points;
try {
//Changed This
/*final String result = await platform.invokeMethod('getBatteryLevel');
batteryLevel = 'Battery level at $result % .'; */
//To This
final List<double> result = await platform.invokeMethod('getBatteryLevel');
points = result;
print("Message 2");
Then In MainActivity.java, changed the getBatteryLevel function to this:
private List<Double> getBatteryLevel() {
List<Double> list = Arrays.asList(1.38, 2.56, 4.3);
return list;
And Lastly changed the MethodCall to this:
List<Double> batteryLevel = getBatteryLevel();
Log.i("errors", "Message");
result.success(batteryLevel);
But when I run the code, the Message 1 gets printed but Message 2 does not. I noticed that with simple types like Strings and Integer instead of List worked. I also tried to replace List with arrays in java, but still didn't work. The Problem is that I don't get any error any stacktraces, anything. What I thought is that in dart he's waiting for something that never comes as Lists of doubles in Dart and Java aren't the same. But it's just a thing I thought. Could it be the problem? How Can I solve it?
Try to receive the List
without <double>
because Dart assumes the type inside the List
is dynamic and not primitive, so you have an error when you try to parse List<dynamic>
into List<double>
.
final List result = await platform.invokeMethod('getBatteryLevel');
Should work.
Don't forget to include your imports in your MainActivity, looks like you are using Arrays
and List
classes, then import the following classes:
import java.util.Arrays;
import java.util.List;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.