MissingPluginException(No implementation found for method methodName on channel channelName

Viewed 742

At root where main() resides, I've been trying to call MethodChannel and eventually call invokeMethod of it so that I can call one of my methods in the iOS platform code and get the results which in turn will be taken care of by a handler as follows:

const methodChannel = MethodChannel('iOSFlutterChannel');

Future<void> getDataFromPlatform() async {
  try {
    methodChannel.setMethodCallHandler(methodCallHandler);
    await methodChannel.invokeMethod('getData'); // * Where the error occurs
  } on PlatformException catch (e) {
    debugPrint('${e.message}');
  }
}


Future<dynamic> methodCallHandler(MethodCall call) async {
  switch (call.method) {
    case 'abcdef':
      // Do something
      return Future.value('');
  }
}

However, whenever the program comes to the invokeMethod part, I get an error saying "Unhandled Exception: MissingPluginException(No implementation found for method getData on channel iOSFlutterChannel)."

These pieces of code will not trigger an error when they are placed inside a StatefulWidget.

The reason why I put the methods at root is that there is a timer I would like to keep no matter where I am in the app.

What am I missing?

I've decided to use this package and followed its example as much as I can.

flutter_background_service | Flutter Package https://pub.dev/packages/flutter_background_service/example

My goal is that while keeping receiving results inside methodCallHandler, I can forward them back to a StreamBuilder of the page where the timer was started.

To recap the requirements,

  • a single stateful widget page with start/stop button
  • user can go any other pages and the app can go off foreground while the timer is on
  • timer can constantly call invokeMethods like every seconds
  • all results can be forwarded to the original stateful widget page

I've seen some example apps with a timer. But they are always a super simple single-page app. Not really like mine. How would you go about this?

UPDATE: I tried some other packages/examples, but the same "Unhandled Exception: MissingPluginException(No implementation found for method getData on channel iOSFlutterChannel)" follows. So if I could pass this problem, it would be a big step forward to me. Allow me to reduce the scope of the question: How do I call MethodChannel.invokeMethod at top level without getting the aforementioned error?

0 Answers
Related