gpt4 book ai didi

dart - 将BLoC的接收器与另一个BLoC连接

转载 作者:行者123 更新时间:2023-12-03 03:17:36 25 4
gpt4 key购买 nike

我正在使用the Google IO talk中描述的BLoC模式。

我有一个简单的BLoC,每当将字符串添加到messageSink时,该BLoC便会在UI中显示警报:

class AlertBloc {
final _message = BehaviorSubject<String>();

AlertBloc() {}

Stream<String> get message => _message.stream;

Sink<String> get messageSink => _message.sink;

void dispose() {
_message.close(); }
}

在应用程序的其他地方,我还有另一个BLoC,当满足特定条件时,该BLoC需要向 messageSink添加字符串。

我注意到从 Google I/O repo for the talk提供整个BLoC并不是一个好主意,并且他们提供了有关将BLoC的 连接到另一个BLoC 接收器的建议:

Note that we are not providing [CartBloc] to the [ProductSquareBloc] directly, although it would be easier to implement. BLoCs should not depend on other BLoCs (separation of concerns). They can only communicate with each other using streams. In this case, the [CartBloc.items] output plugs into the [ProductSquareBloc.cartItems] input.



我的问题是如何将BloC中的 接收器连接到另一个BLoC

最佳答案

这是一个简单的例子。想象以下两个BLoC:

第一个公开Stream并用一些值填充它:

class ProducerBLoC {

//Controller is private - you do not want to expose it
final StreamController<int> _productionController = StreamController<int>();
//Instead, you expose a stream
Stream<int> get production => _productionController.stream;

//This method generates some values and puts them to stream
void produceValue() {
_productionController.sink.add(1);
_productionController.sink.add(2);
_productionController.sink.add(3);
}

//Don't forget to close your controllers
void dispose() {
_productionController.close();
}
}

另一个公开 Sink并处理放入其中的值。
class ConsumerBLoC {

//Controller is private - you do not want to expose it
final StreamController<int> _consumptionController = StreamController<int>();
//Instead, you expose a sink
StreamSink<int> get consumption => _consumptionController.sink;

//In class constructor we start listening to the stream of values
ConsumerBLoC() {
_consumptionController.listen((value) {_consumeValue(value);} );
//or simply: _consumptionController.listen(_consumeValue); //theese are the same
}

//This method generates some values and puts them to stream
void consumeValue(int value) {
//Do something with the value
print('Value processed: $value');
}

//Don't forget to close your controllers
void dispose() {
_consumptionController.close();
}
}

现在,任务是将生产流连接到消耗接收器。正如您已经正确注意到的那样,您不希望两个BLoC中的任何一个都知道另一个BLoC的存在。因此,这两者都不应该保留对另一个对象的引用,甚至不应该创建另一个对象的实例。而是使用 Widget类将它们连接起来:
//Define some widget to represent main screen of your application
class MainScreen extends StatefulWidget {

@override
State<StatefulWidget> createState() => _MainScreenState();
}

//And define a state for this widget (state does not need to be public)
class _MainScreenState extends State<MainScreen> {

//You define both blocks here
ProducerBLoC _producer = new ProducerBLoC();
ConsumerBLoC _consumer = new ConsumerBLoC();

//Now, either do it in _MainScreenState constructor, or in the initState() method
@override
void initState() {
super.initState();

//Connect production stream with consumption sink
_producer.production.listen((value) => _consumer.consumption.add(value));
//Or, beautifully: _producer.production.pipe(_consumer.consumption);
}

@override
Widget build(BuildContext context) {
//The exact implementation does not matter in current context
}

//And don't forget to close your controllers
@override
dispose() {
super.dispose();
_producer.dispose();
_consumer.dispose();
}

}

这样, ProducerBLoC会立即消耗 ConsumerBLoC生成的任何值。而且,最重要的是-两个BLoC彼此完全独立!

关于dart - 将BLoC的接收器与另一个BLoC连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53254371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com