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