Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send data back from iOS to Flutter?

I am trying to get iOS to send data back to flutter. More specifically the Control Center Media Controls. I am working on a music app and I can get data sent from Flutter to iOS, thus allowing it to be displayed in the Media Controls.

However, how would I get iOS to send data back on its own channel if I were to control play pause next previous? Basically have Flutter listen to iOS sending data.

like image 276
punjabi4life Avatar asked Sep 05 '25 05:09

punjabi4life


1 Answers

Assuming that you are using Swift in your plugin (called XXX), you will have a SwiftXXXPlugin class, with a static register method. Move channel to become a static, rather than a local variable of register. Then create some static methods for your iOS to Dart methods and call invokeMethod like this:

channel.invokeMethod("someMethod", arguments: "someValue")

arguments is Any and could be anything that the channel knows how to encode, for example, byte array, String, int, double, etc. It can also encode lists and maps of the basic objects.

At the Dart end, you have XXX.dart. Add a static method called, for example, setHandler, and implement the implementations of the methods. You will need to call setHandler once before using the channel.

  static setHandler() {
    _channel.setMethodCallHandler(methodCallHandler);
  }

  static Future<dynamic> methodCallHandler(MethodCall methodCall) async {
    switch (methodCall.method) {
      case 'someMethod':
        print(methodCall.arguments); // prints the argument - "someValue"
        return null; // could return a value here

      default:
        throw PlatformException(code: 'notimpl', message: 'not implemented');
    }
  }
like image 86
Richard Heap Avatar answered Sep 07 '25 21:09

Richard Heap