How to detect a call in a background and make a bridge between native and dart code How to detect a call in a background and make a bridge between native and dart code dart dart

How to detect a call in a background and make a bridge between native and dart code


You can create MethodChannel between native and dart. Here's sample code :

    String channel = "my.data";    MethodChannel methodChannel = new MethodChannel(Objects.requireNonNull(getFlutterEngine()).getDartExecutor().getBinaryMessenger(), channel);    final HashMap<String, String> map = new HashMap<>();    map.put("yourvar", "myvar");    methodChannel.setMethodCallHandler(            new MethodChannel.MethodCallHandler() {                @Override                public void onMethodCall(MethodCall call, MethodChannel.Result result) {                    if (call.method.equals("onFinish")) {                        finish();                    }                }            });    methodChannel.invokeMethod("callMyFunction", map);

In your dart file write this code,

static const platform = const MethodChannel('my.data"');  _MyPageState() {    platform.setMethodCallHandler(_receiveFromNative);  }  Future<void> _receiveFromNative(MethodCall call) async {    try {      print(call.method);      if (call.method == "callMyFunction") {        print(call.arguments['yourvar']);        //write your code here       }      } on PlatformException catch (e) {}   }

You can refer to this document as well