3

I'm trying to create a channel between Flutter and Android.

I have this code inside MainActivity and this channel just works fine when called from FLutter code.

setMethodCallHandler { call, result ->
            Log.d("configureFlutterEngine", "${call.method} $result $call")
            if (call.method == "getBatteryLevel") {
                val batteryLevel = getBatteryLevel()

                if (batteryLevel != -1) {
                    result.success(batteryLevel)
                } else {
                    result.error("UNAVAILABLE", "Battery level not available.", null)
                }
            } else if (call.method == "startTrackingService") {
                Log.d("startTrackingService", "calling foregroundService()")
                foregroundService()
                result.success(true)
            } else if (call.method == "stopTrackingService") {
                Log.d("stopTrackingService", "calling stopTrackingService()")
                stopTrackingService()
                result.success(true)
            } else {
                result.notImplemented()
            }
        }

But it is not working the other way i.e. Native to FLutter. I have this code in My android service onCreate().

FlutterLoader().apply {
        startInitialization(context)
        ensureInitializationComplete(context, arrayOfNulls(0))

        val engine = FlutterEngine(context.applicationContext)
        val entrypoint = DartEntrypoint.createDefault()

        engine.dartExecutor.executeDartEntrypoint(entrypoint)
        methodChannel = MethodChannel(engine.dartExecutor.binaryMessenger, "android-flutter")

And this is called when everytime I get the location.

        methodChannel?.invokeMethod("getTracking", position.toJson(), object: MethodChannel.Result {
            override fun success(result: Any?) {
                Log.d("success", result.toString())
            }

            override fun error(errorCode: String?, errorMessage: String?, errorDetails: Any?) {
                Log.d("error", errorDetails.toString())
            }

            override fun notImplemented() {
                Log.d("notImplemented", "notImplemented".toString())
            }

        })
    }

And I have this in Flutter Widget.

void initState() {
  super.initState();
  MethodChannel("android-flutter").setMethodCallHandler((call) async {
    Fimber.i("$call invoked");
    if (call.method == "getTracking") {
      if (call.arguments is String) {
        Fimber.i(call.arguments);
        Position position = Position.fromJsonString(call.arguments);
        Fimber.i(position.toString());
      }
    } else {
      throw MissingPluginException();
    }
    return true;
  });
}

The invokeMethod(...) is getting called inside the service, but the method definition in flutter is not getting called.. After everytime invokeMethod() in service is called, The overridden notImplemented() method under the Result(){} is getting called.

  • 1
    it's easier to use `EventChannel` in such case – pskink May 06 '21 at 09:18
  • Tried it.. Not working when I have `setStreamHandler()` in the Service because `receiveBroadcastStream()` is called when the widget is created i.e. before the service is created. So for now, calling it in MainActivity and using a global MutableLiveData. Any suggestions to overcome this? – Vishak A Kamath May 06 '21 at 10:55
  • so `receiveBroadcastStream` throws some exception? "not working" does not say much – pskink May 06 '21 at 11:00
  • Ohh sorry about that. Getting this Exception `MissingPluginException(No implementation found for method listen on channel android-flutter)` because by the time the `setStreamHandler()` is called, `receiveBroadcastStream()` is already called and hence the exception – Vishak A Kamath May 07 '21 at 06:19

0 Answers0