stickyBroadcast method

void stickyBroadcast(
  1. String key, {
  2. dynamic value,
  3. ValueCallback? callback,
  4. bool persistence = false,
})

广播一条 key 类型的粘性消息。 如果广播系统中还有没注册该类型的接收者,本条消息将被滞留在系统中。一旦有该类型接收者被注册,本条消息将会被立即发送给接收者。 如果系统中已经注册有该类型的接收者,本条消息将会被立即发送给接收者。 接收者通过 value 可以获取到本条消息携带的数据。 key - 消息类型 value - 消息携带的数据。可以是任意类型或是null。 callback - 能够收到接收器返回的消息 persistence - 是否持久化消息类型。持久化的消息可以在任意时刻通过 FBroadcast.value 获取当前消息的数据包。默认情况下,未持久化的消息类型在没有接收者的时候会被移除,而持久化的消息类型则不会。开发者可以通过 clear 函数来移除持久化的消息类型。

Broadcast a sticky message of type key. If there are unregistered receivers of this type in the broadcast system, this message will be stuck in the system. Once a recipient of this type is registered, this message will be sent to the recipient immediately. If this type of receiver is already registered in the system, this message will be sent to the receiver immediately. The receiver can get the data carried in this message through value.

key - Message type value - The data carried in the message. Can be any type or null. callback - Able to receive the message returned by the receiver persistence - Whether or not to persist message types. Persistent messages can be retrieved at any time by FBroadcast. Value for the current message packet. By default, unpersisted message types are removed without a receiver, while persisted message types are not. Developers can use the clear function to remove persistent message types.

Implementation

void stickyBroadcast(String key,
    {dynamic value, ValueCallback? callback, bool persistence = false}) {
  if (_map == null) return;
  if (_textIsEmpty(key)) return;
  if (persistence && !_get(key).persistence) {
    _get(key).persistence = true;
  }
  if (_map.containsKey(key) && _map[key]!.hasListeners) {
    broadcast(key, value: value, callback: callback);
  } else {
    if (_stickyMap[key] == null) {
      _stickyMap[key] = [];
    }
    _stickyMap[key]!.add(_Notifier(value)..callback = callback);
  }
}