简介

一个用于 flutter 中进行数据通信和交互的通用的包。使用方法类似于 iOS 中的 NSNotificationCenter,使用方便,但是注意释放资源。

使用

使用步骤,同 iOS 中的通知消息:

  1. 添加观察器
  2. 发送通知
  3. 销毁时,释放观察器
    NotificationCenter.instance.addObserver(object1, key: 'key1', callback: (message) {
      print('message object1 = $message');
      curMessage = message;
    });

    NotificationCenter.instance.addObserver(object2, key: 'key2', callback: (message) {
      print('message object2 = $message');
      curMessage = message;
    });

    NotificationCenter.instance.post(key: 'key1', data: 'post data1');
    expect(curMessage, 'post data1');
    NotificationCenter.instance.post(key: 'key2', data: 'post data2');
    expect(curMessage, 'post data2');

    NotificationCenter.instance.removeObserver(object1);
    NotificationCenter.instance.post(key: 'key1', data: 'post data1');
    expect(curMessage, 'post data2');

    NotificationCenter.instance.post(key: 'key2', data: 'post data3');
    expect(curMessage, 'post data3');

Libraries

z_notification