【问题标题】:Dart / rxdart / Bloc: receive event item with original type when listening to a BehaviorSubject's streamDart / rxdart / Bloc:在监听 BehaviorSubject 的流时接收具有原始类型的事件项
【发布时间】:2019-04-14 04:50:42
【问题描述】:

考虑这段代码(不要介意listen这个没用的方法,它只是为了展示用例):

class Bloc {
  final BehaviorSubject notifPrompt =
    BehaviorSubject<NotifPromptModel>()..add(NotifPromptModel(answered: false));

  void listen() {
    notifPrompt.stream.listen(
      (data) => print(data.answered)
    );
  }

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

class NotifPromptModel {
  final bool answered;

  NotifPromptModel({this.answered});
}

现在我知道这会起作用,但是在这种情况下,有没有办法获得generic typeNotifPromptModel,我将其传递给BehaviorSubjectStreamController,每一次发送最后一个event带有data 参数的新listen)?当我将包含model 信息的object 作为fields 传递给BehaviorSubject 时,这将允许我获得方便的代码建议,就像在这种情况下一样。

【问题讨论】:

    标签: stream dart flutter rxdart


    【解决方案1】:

    应该多花几分钟时间思考。问题是我用type 实例化了BehaviorSubjectobject,但没有用它实例化declare variable

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-09
      • 2020-06-18
      • 2019-08-04
      • 2022-06-26
      • 1970-01-01
      • 2021-06-27
      • 2017-05-18
      • 2020-02-18
      相关资源
      最近更新 更多