4

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.

Samet ÖZTOPRAK
  • 3,112
  • 3
  • 32
  • 33
punjabi4life
  • 655
  • 1
  • 9
  • 22

1 Answers1

3

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');
    }
  }
Richard Heap
  • 48,344
  • 9
  • 130
  • 112
  • 1
    Sweet!!! This is what I wanted, sucks that there is no examples by the Flutter team for this. Many Many Thanks – punjabi4life Feb 16 '19 at 00:16
  • You mean, other than the one provided by the Flutter team at https://flutter.io/docs/development/platform-integration/platform-channels ? – Randal Schwartz Feb 16 '19 at 20:44
  • 3
    Yeah, that one only talks about getting data back from the Platform by Flutter. It does explain how to send data to Flutter by Platform. Like Media Notification to pause a song, Control center pause button to pause a song. Both of which are platform specific things. – punjabi4life Feb 18 '19 at 11:43