我如何才能使其他流的新流返回所有发出的值的列表(bloc模式)? [英] How can i make a new stream of other streams returning a list of all the values emitted (bloc pattern)?
问题描述
我有3条这样的视频流:
I have three streams like this:
final _light1RoomCtlr = BehaviorSubject<String>();
final _light2RoomCtlr = BehaviorSubject<String>();
final _light3RoomCtlr = BehaviorSubject<String>();
Stream<String> get getLight1Room => _light1RoomCtlr.stream;
Function(String) get setLight1Room => _light1RoomCtlr.sink.add;
Stream<String> get getLight2Room => _light2RoomCtlr.stream;
Function(String) get setLight2Room => _light2RoomCtlr.sink.add;
Stream<String> get getLight3Room => _light3RoomCtlr.stream;
Function(String) get setLight3Room => _light3RoomCtlr.sink.add;
我想将它们全部合并,以便我可以在需要时获取所有发出的值.
I want to merge them all so i can get all the values emitted when i need it.
问题是..我正在使用块模式,并且我没有initState()函数,所以我无法初始化东西..因此,dart不允许我在同一位置做类似的事情文件:
The thing is.. i am using bloc pattern and i don't have a initState() function on it, so i can't initialize stuff.. so dart won't let me do something like this on the same file:
final List<String> _lightRooms = List<String>();
Observable.merge([getLight1Room, getLight2Room, getLight3Room]).listen((room) =>
_lightRooms.add(room);
));
我已经尝试了很多事情,并且不断收到错误消息:只有静态成员才可以初始化..或类似的东西.如何使用反应式编程和块模式进行此操作?如果我在需要的小部件上进行监听,则可能会丢失另一小部件上的信息.我尝试了rxjs示例,但是在这里它不起作用,因为这仅仅是类定义.
I have tried a lot of things and i keep getting the error message: only static members are initializable.. or something like that.. How can i procede this using reactive programming and bloc pattern? If i do the listening on a widget that i need it, i can lose the info on another one. i tried rxjs examples but here it does not work since this is only class definition things.
推荐答案
您可以使用CombineLatest2,combinateLatest4,combinateLatest5等
You can user combineLatest2, combineLatest4, combineLatest5 and so on
例如:
import 'dart:async';
import 'package:login_bloc/src/blocs/validators.dart';
import 'package:rxdart/rxdart.dart';
class Bloc with Validators {
final _email = BehaviorSubject<String>();
final _password = BehaviorSubject<String>();
Stream<String> get email => _email.stream.transform(validateEmail);
Stream<String> get password => _password.stream.transform(validatePassword);
Stream<bool> get submitValid =>
Observable.combineLatest2(email, password, (e, p) => true);
Function(String) get changeEmail => _email.sink.add;
Function(String) get changePassword => _password.sink.add;
submit() {
final validEmail = _email.value;
final validPassword = _password.value;
print('valid data:');
print(validEmail);
print(validPassword);
}
dispose() {
_email.close();
_password.close();
}
}
这篇关于我如何才能使其他流的新流返回所有发出的值的列表(bloc模式)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!