当错误发生时,变换流值不是无效的,而转换应使其无效
有一个流的流不正确,因为我将在转换过程中添加null。 但是转换变化不会影响行为求主。值为norciy buciviourSubject.stream.value
BehaviorSubject _roomAmount = BehaviorSubject<String>();
Stream<String> get roomAmount$ => _roomAmount.stream.transform(validateRoomsAmount);
final validateRoomsAmount = StreamTransformer<String, String>.fromHandlers(
handleData: (value, sink){
//Add null before
sink.add(null);
if(value == null || value == '') {
return;
}
//when it is wrong add error and return
if(value.length > 9) {
sink.addError("Too long");
return;
}
//when it is right add value into sink
sink.add(size.toString());
);
House getHouse() {
//_roomAmount.value returns the value which has a length over 9, which should have been transformed to null
return House(roomAmount: _roomAmount.value);
}
当我从流中创建对象时,它的值错误。显然,似乎并没有介绍该值在变换函数中以空白为null。
我认为我不完全理解如何从chapioUrsubject中创建一个对象。
当我输入转换功能时,每个符号都会调用。
流正确显示错误具有NULL的数据,这也是预期的行为。
只有当我要从流中创建一个对象时,才是意外的值。
我使用 rxdart:0.24.1
扑动医生-v
[√]扑来(频道未知,1.26.0-17.8.pre,在Microsoft Windows上[版本10.0.19044.1706],locale de-de)
•flutter版本1.26.0-17.8. pre at C:\ flutter
•框架修订版044F2CF560(1年,3个月前),2021-02-24 13:02:05 -0800
•发动机修订042C82B02C
•DART版本2.12.0(构建2.12.0-259.16.beta)
Have a Stream which can be incorrect therefor i will sink add null in it during the transform.
But the transformation change doesnt affect the BehaviourSubject.value nor BehaviourSubject.stream.value
BehaviorSubject _roomAmount = BehaviorSubject<String>();
Stream<String> get roomAmount$ => _roomAmount.stream.transform(validateRoomsAmount);
final validateRoomsAmount = StreamTransformer<String, String>.fromHandlers(
handleData: (value, sink){
//Add null before
sink.add(null);
if(value == null || value == '') {
return;
}
//when it is wrong add error and return
if(value.length > 9) {
sink.addError("Too long");
return;
}
//when it is right add value into sink
sink.add(size.toString());
);
House getHouse() {
//_roomAmount.value returns the value which has a length over 9, which should have been transformed to null
return House(roomAmount: _roomAmount.value);
}
When I create an object from my stream it has the wrong value in it. Apperantly it doesn't seem to interesst that the value was sinked as null in the transform function.
I think i dont fully understand how I should create an object from BehaviourSubject.
When I type transformation function gets called with each sign.
Stream displayes error correctly has a data of null, which is also expected behaviour.
Only when i am about to create an object out of the stream it is the unexpected value.
I use
rxdart: 0.24.1
flutter doctor -v
[√] Flutter (Channel unknown, 1.26.0-17.8.pre, on Microsoft Windows [Version 10.0.19044.1706], locale de-DE)
• Flutter version 1.26.0-17.8.pre at c:\flutter
• Framework revision 044f2cf560 (1 year, 3 months ago), 2021-02-24 13:02:05 -0800
• Engine revision 042c82b02c
• Dart version 2.12.0 (build 2.12.0-259.16.beta)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否考虑过使用
flatmap
和在其中
实现这一目标而无需转换,这使您的代码可读性非常困难。尝试以下操作:
其中
:确保您的流仅收到非null或空白文本。flatmap
:检查长度和返回错误或值。Have you considered using
flatMap
andwhere
to achieve that without the need to transformation which makes your code readability very difficult.try this:
where
: ensures your stream will receive only non null or blank text.flatMap
: checks the length and return error or value.