我有以下三个流:
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;
我想将它们全部合并,以便我可以在需要时获取所有发出的值。
问题是..我正在使用块模式,并且我没有initState()函数,所以我无法初始化东西..所以dart不允许我在同一位置做类似的事情文件:
final List<String> _lightRooms = List<String>();
Observable.merge([getLight1Room, getLight2Room, getLight3Room]).listen((room) =>
_lightRooms.add(room);
));
我已经尝试了很多事情,并且不断收到错误消息:只有静态成员才能初始化..或类似的东西。 如何使用反应式编程和块模式进行此操作?如果我在需要的小部件上进行监听,则可能丢失其他信息。我尝试了rxjs示例,但是在这里它不起作用,因为这只是类定义。
答案 0 :(得分:0)
您可以使用CombineLatest2,combinateLatest4,combinateLatest5等
例如:
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();
}
}