flutterkotlinflutter-platform-channel

How to pass kotlin FloatArray to flutter?


I am trying to implement some kotlin native code so I have a FloatArray which I need to pass to flutter side.

Here is what it looks like,

kotlin code snippets

private var sampleData : FloatArray? = null

fun someFunction(){
// add data to sampleData
}

fun getData(result: MethodChannel.Result) {
        result.success(sampleData)
    }

dart code snippet

Future<void> getData() async {
    var result = await _methodChannel.invokeMethod('sampleData');
    print('type -> ${result.runtimeType}');
  }

When I print on both platforms it does print data on both platforms but on the flutter side when I check the runtimeType it always says List<Object?>.

So how can I pass FloatArray so that object is the relevant type?


Solution

  • I solved it by using List.from() like this,

    Future<List<double>> getData() async {
        var result = await _methodChannel.invokeMethod('sampleData');
        return List<double>.from(result ?? []);
      }