Flutter native plugin:使用eventlisteners的示例

时间:2017-09-17 11:24:30

标签: android ios plugins flutter

我正在寻找一个易于理解的带有原生插件的eventlisteners示例。例如:我写的插件有很多听众在行动失败或完成时做某事。那些监听器可以用本机代码编写,但是我如何允许编写dart代码的最终用户编写自己的自定义监听器?例如,在onSuccess事件中添加另一个变量赋值?

我正在为Android和iOS搜索一个小例子。

----编辑----
我正在使用此代码atm:

public static void registerWith(Registrar registrar) {
    final MethodChannel methodChannelGeoFire = new MethodChannel(registrar.messenger(), "com.bram.vanbilsen.geofire.GeoFire");
    final MethodChannel methodChannelGeoQuery = new MethodChannel(registrar.messenger(), "com.bram.vanbilsen.geofire.GeoQuery");
    final EventChannel geoQueryKeyEnteredEventChannel = new EventChannel(registrar.messenger(), "com.bram.vanbilsen.geofire.keyEnteredChannel");
    final EventChannel geoQueryKeyExitedEventChannel = new EventChannel(registrar.messenger(), "com.bram.vanbilsen.geofire.keyExitedChannel");
    final EventChannel geoQueryKeyMovedEventChannel = new EventChannel(registrar.messenger(), "com.bram.vanbilsen.geofire.keyMovedChannel");
    final EventChannel geoQueryQueryReadyEventChannel = new EventChannel(registrar.messenger(), "com.bram.vanbilsen.geofire.queryReadyChannel");
    final EventChannel geoQueryQueryErrorEventChannel = new EventChannel(registrar.messenger(), "com.bram.vanbilsen.geofire.queryErrorChannel");
    final GeofirePlugin instance = new GeofirePlugin(registrar.activity());
    geoQueryKeyEnteredEventChannel.setStreamHandler(instance);
    geoQueryKeyExitedEventChannel.setStreamHandler(instance);
    geoQueryKeyMovedEventChannel.setStreamHandler(instance);
    geoQueryQueryReadyEventChannel.setStreamHandler(instance);
    geoQueryQueryErrorEventChannel.setStreamHandler(instance);
    methodChannelGeoFire.setMethodCallHandler(instance);
    methodChannelGeoQuery.setMethodCallHandler(instance);
}

GeofirePlugin(Activity activity) {
  this.activity = activity;
}

private final Activity activity;

if (call.method.equals("addGeoQueryEventListener")) {
  String refPath = call.argument("refPath");
  DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
  GeoFire geofire = new GeoFire(ref);
  List<Double> center = call.argument("center");
  double radius = call.argument("radius");
  GeoQuery query = geofire.queryAtLocation(new GeoLocation(center.get(0), center.get(1)), radius);
  query.addGeoQueryEventListener(new GeoQueryEventListener() {
    @Override
    public void onKeyEntered(String key, GeoLocation location) {
      System.out.println("entered");
    }

    @Override
    public void onKeyExited(String key) {
      System.out.println("exited");
    }

    @Override
    public void onKeyMoved(String key, GeoLocation location) {
      System.out.println("moved");
    }

    @Override
    public void onGeoQueryReady() {

    }

    @Override
    public void onGeoQueryError(DatabaseError error) {
      System.out.println(error.getMessage());
    }
  });
}

我的onListenonCancel方法仍为空。我想先设置geoQueryQueryReadyEventChannel。我希望onGeoQueryReady中的GeoQueryEventListener选择某个内容,然后通过此频道发送数据。我怎样才能做到这一点?我的主要问题是,我不明白如何将GeoQueryEventListener方法中的onListen链接到本地​​GeoQuery实例。

2 个答案:

答案 0 :(得分:3)

不确定这是否有帮助,但我最近编写了一个包含多个事件监听器的插件:https://github.com/apptreesoftware/pusher_flutter。当我编写它时,我使用传感器插件作为指南:https://github.com/flutter/plugins/tree/master/packages/sensors

基础是为您创建FlutterEventChannel所需的每个侦听器和另一个实现FlutterStreamHandler的类。

在我的示例中,我创建了3个频道及其StreamHandler。我有一个用于侦听错误的通道,一个用于连接更改,另一个用于接收消息。每个EventChannel都有一个标识符。在我的dart代码中,我为在本机代码中创建的每个FlutterEventChannel创建了一个事件通道。

_connectivityEventChannel =
        new EventChannel('plugins.apptreesoftware.com/pusher_connection');
_messageChannel =
        new EventChannel('plugins.apptreesoftware.com/pusher_message');
_errorChannel =
        new EventChannel('plugins.apptreesoftware.com/pusher_error');

然后我使用`receiveAsBroadcastStream:

公开API是Streams
Stream<PusherMessage> get onMessage =>
      _messageChannel.receiveBroadcastStream().map(_toPusherMessage);

Stream<PusherError> get onError =>
      _errorChannel.receiveBroadcastStream().map(_toPusherError);

所以最终用户API是:

pusher.onMessage.listen((m) => doSomething(m));
pusher.onError.listen((e) => doSomethingWithError(m));

答案 1 :(得分:1)

希望这会有所帮助...

<group_name1>

GeoFirePlugin如下:

EventChannel eventChannelService = new 
        EventChannel(registrar.messenger(), "com.xyz");
eventChannelService.setStreamHandler(
        new GeofirePlugin(registrar.activity()));