1

I'm wondering if the following code is memory safe:

Future<String> getValueFromNative() async{
    const channel = MethodChannel("channelName");
    var result = await channel.invokeMethod("myMethod");
    
    return result;
}

I wonder if I will be leaking memory if I create a new method channel each time I need to call to the native? Is it necessary to cache the created channel?

Consider another scenario, supposed I created a channel that is supposed to be called from the native part:

channel.setMethodCallHandler((call){
      print(call.method);
});

Should the channel object still be alive at the time of the call? Or the handler will somehow be registered to the binary messager thus the channel object itself is safe to be discarded?

I'm trying to find some discussion on this but unfortunately can't find any.

Thanks.

Mycotina
  • 369
  • 3
  • 11

0 Answers0