Is there, in flutter, a way of calling an init function in the plugin automatically on startup of an app?

不羁的心 提交于 2020-03-05 03:06:08

问题


Since this is a flutter plugin, I don't want developers depending on the plugin to have to init the plugin in anyway. Maybe they'll have to, but I'd really like for them not to have to.


Basically, I want a way of calling the _channel.setMethodCallHandler method automatically when the app depending on the plugin starts. Without other developers having to do so, for instance calling a static method living in the DragndropPlugin class, themselves to "initialize" the plugin.

Maybe there are some kind of way of replicating the way this is done on the platform specific side in Java. Here's an example of the method that gets called, I guess by flutter somehow, whenever the plugin "loads":

public static void registerWith(Registrar registrar) {
    DragndropPlugin.methodChannel = new MethodChannel(registrar.messenger(), CHANNEL);
    DragndropPlugin.methodChannel.setMethodCallHandler(new DragndropPlugin());
}

Specifically, I'm talking about the registerWith method.


I tried the following, but this doesn't seem to work. Not the main question but I would appreciate an explanation to why this wouldn't work. Or for that matter, if this should work, potential errors I might have made for it not to work.

abstract class DragndropPlugin {

  static const MethodChannel _channel =
      const MethodChannel(_CHANNEL);

  static void _handler = _channel.setMethodCallHandler(DragndropPlugin._onMethodCall);

  static Future<dynamic> _onMethodCall(MethodCall call) async {
    switch (call.method) {
      case "onDrag":
        print(call.arguments.toString());
        break;
      default:
        print("An unknown method was invoked on the platform-specific side.");
        break;
    }
  }
}


My question is basically how I would go about achieving the above.


Flutter 1.9.1+hotfix.6 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 68587a0916 (6 weeks ago) • 2019-09-13 19:46:58 -0700
Engine • revision b863200c37
Tools • Dart 2.5.0


回答1:


Presumably, you are gong to want to do more than print(call.arguments.toString()) in your method handler! You will likely want to invoke some callback on the plugin user's code. The user will have had to register this callback with your plugin at some point, and this is where you should do your initialization.

The premise of your question is that you don't want the user to have to initialize the plugin, but they need somewhere to register the functions or interface that you will call in response to native->Dart method calls. This is, therefore, one and the same initialization method!

Here's an example from a typical plugin, called SomePlugin that defines an abstract class SomePluginEvents of which the user must provide a concrete instance, to receive the native->Dart methods / events.

  static setEventHandler(SomePluginEvents handler) {
    if (handler == null || SomePlugin.handler != null) {
      throw Exception('cannot set event handler');
    }
    _channel.setMethodCallHandler(methodCallHandler);
    SomePlugin.handler = handler;
  }

To answer your subsidiary point, Dart is designed that nothing runs unless invoked directly from main. _channel is initialized when first used, but since nobody ever refers to _handler it's never initialized. There is no static initialization in Dart (unlike Java, say).



来源:https://stackoverflow.com/questions/58573536/is-there-in-flutter-a-way-of-calling-an-init-function-in-the-plugin-automatica

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!