当错误发生时,变换流值不是无效的,而转换应使其无效

发布于 2025-02-04 00:27:50 字数 1376 浏览 6 评论 0原文

有一个流的流不正确,因为我将在转换过程中添加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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┈┾☆殇 2025-02-11 00:27:50

您是否考虑过使用flatmap在其中实现这一目标而无需转换,这使您的代码可读性非常困难。

尝试以下操作:

  final _roomAmount = BehaviorSubject<String>();

  Stream<String> get roomAmount => _roomAmount.stream
          .where((value) => !value.isNullOrBlank)
          .flatMap((value) {
        if (value.length > 9) return Stream.error("Too long");
        return Stream.value(value);
      });

其中:确保您的流仅收到非null或空白文本。

flatmap:检查长度和返回错​​误或值。

Have you considered using flatMap and where to achieve that without the need to transformation which makes your code readability very difficult.

try this:

  final _roomAmount = BehaviorSubject<String>();

  Stream<String> get roomAmount => _roomAmount.stream
          .where((value) => !value.isNullOrBlank)
          .flatMap((value) {
        if (value.length > 9) return Stream.error("Too long");
        return Stream.value(value);
      });

where: ensures your stream will receive only non null or blank text.

flatMap: checks the length and return error or value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文