何在Flutter中将事件从一个有状态的小部件广播到另一个小部件

何在Flutter中将事件从一个有状态的小部件广播到另一个小部件

本文介绍了如何在Flutter中将事件从一个有状态的小部件广播到另一个小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个事件从一个有状态的小部件广播到另一个状态,但是似乎找不到解决方法.我安装了此插件:事件:^ 1.1.4,但未触发.我想要类似以下的内容:

I would like to broadcast an event from one Stateful Widget to another but cant seem to find a way to do. I installed this plugin : event: ^1.1.4 but it doesn't fire. I want something like below:

Stateful Widget 1:
SomeEventClass.broadcastEvent();

Stateful Widget 2:
SomeEventClass.subscribe();

推荐答案

使用类似的方法.

class ContinuousStream {
  _DataStore _mStorage = _DataStore.getInstance();

  /// Sets a new value [value] to the data stream [stream].
  /// If there are active subscribers, the value will be dispatched to them.
  void emit(String stream, var value) {
    _mStorage?.setValue(stream, value);
  }

  /// Subscribes to the given stream [stream].
  /// If stream already has data set, it will be delivered to the [callback] function.
  void on(String stream, void Function(Object) callback) {
    _mStorage?.setCallback(stream, callback);
  }

  /// Returns the current value of a given data [stream].
  Object getValue(String stream) {
    return _mStorage?.getValue(stream);
  }
}

// Storage class for ContinuousStream.
class _DataStore {
  // Singleton Instance for DataStore
  static _DataStore _instance;

  // Map instance to store data values with data stream.
  HashMap<String, _DataItem> _mDataItemsMap = HashMap();

  // Sets/Adds the new value to the given key.
  void setValue(String key, var value) {
    // Retrieve existing data item from map.
    _DataItem item = _mDataItemsMap[key];

    item ??= _DataItem();

    // Set new value to new/existing item.
    item.value = value;

    // Reset item to the map.
    _mDataItemsMap[key] = item;

    // Dispatch new value to all callbacks.
    item.callbacks?.forEach((callback) {
      callback(value);
    });
  }

  // Sets/Adds the new callback to the given data stream.
  void setCallback(String key, Function(Object) callback) {
    if (callback != null) {
      // Retrieve existing data item from the map.
      _DataItem item = _mDataItemsMap[key];

      item ??= _DataItem();

      // Retrieve callback functions from data item.
      List<Function(Object)> callbacks = item.callbacks;

      // Check if callback functions exists or not.
      if (callbacks == null) {
        // If it's null then create new List.
        callbacks = List();

        // Set callback functions list to data item.
        item.callbacks = callbacks;

        // Set the data item to the map.
        _mDataItemsMap[key] = item;
      }

      // Add the given callback into List of callback functions.
      callbacks.add(callback);

      // Dispatch value to the callback function if value already exists.
      if (item.value != null) {
        callback(item.value);
      }
    }
  }

  // Returns current value of the data stream.
  Object getValue(String key) {
    return _mDataItemsMap[key].value;
  }

  // Returns singleton instance of _DataStore
  static _DataStore getInstance() {
    _instance ??= _DataStore();
    return _instance;
  }
}

// Data class to hold value and callback functions of a data stream.
class _DataItem {
  var value;
  List<Function(Object)> callbacks;
}

创建此类后,您可以初始化和访问流.例如,这需要在StateFulWidget 1和StateFUlWidget 2中完成.

Once this class is made then you can initialize and access the stream. For Example This needs to be done in both StateFulWidget 1 and StateFUlWidget 2.

ContinuousStream continuousStream = new ContinuousStream();

然后从StateFulWidget1:

Then from StateFulWidget1:

void send() {
      String message = "Hello";
      continuousStream.emit("chat-message", message);
  }

这对应于您的broadCast事件.

This corresponds to your broadCast event.

从StatefulWidget2:

And from StatefulWidget2:

continuousStream.on("chat-message", (message) {
      print("Message Received: $message");
    });

这对应于您的订阅事件.这应该可以解决您的问题.

This corresponds to your subscribe Event.This should fix your issue.

这篇关于如何在Flutter中将事件从一个有状态的小部件广播到另一个小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 22:15