flutter_js icon indicating copy to clipboard operation
flutter_js copied to clipboard

[iOS] async onMessage callback

Open iulian0512 opened this issue 2 years ago • 1 comments

it would be really useful to be able to run async code in the onMessage callback suppose you need to do some io processing or something else async like requesting user location or anything else.

dart side

 jseng.onMessage('ch1', (args) async {
      print("from ch1:$args");
      print("before delay");
      await Future.delayed(Duration(seconds: 5));
      print("after delay");
      var jsonstr = json.encode({"a1": "b1"});
      return jsonstr;
    });

jscript side

async function test(){ 
              console.log("inside async function test");
             var result= await sendMessage('ch1', '4879');
              console.log('fromsendmessage:'+result);
              return 55; }

the dart code above fails with Could not encode return value of message on channel ch1 to json... returning null

and its because of

     final result = channelFunctions[channelName]!.call(jsonDecode(message));
      try {
        final encoded = json.encode(result);
        return JSValue.makeFromJSONString(context, encoded).pointer;

in flutter_js/lib/javascriptcore/jscore_runtime.dart 186-187 it does not take into account that the channelFunction can be a future.

what i've presented above works ok on android.

iulian0512 avatar Jan 10 '22 15:01 iulian0512

Same here. Is there a work around? I tried using Promises but it does not change anything

acx70 avatar Apr 12 '22 09:04 acx70