简体   繁体   English

Dart / rxdart / Bloc:在收听BehaviorSubject的流时接收具有原始类型的事件项

[英]Dart / rxdart / Bloc: receive event item with original type when listening to a BehaviorSubject's stream

Consider this code (don't mind the useless listen method, it's just to show the use case): 考虑下面的代码(不要介意无用的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});
}

Now I know that this will work, but is there a way to get the generic type , NotifPromptModel in this case, that I pass to the BehaviorSubject ( StreamController that sends last event on every new listen ) with the data parameter? 现在我知道这可以工作,但是有没有办法在这种情况下将generic type NotifPromptModel传递给带有data参数的BehaviorSubject (在每个新的listen上发送最后一个event StreamController )? This would allow me to have convenient code suggestions when I'm passing an object containing the model information as fields to the BehaviorSubject , like in this case. 这样,当我将包含model信息的object作为fields传递给BehaviorSubject ,这将为我提供方便的代码建议,例如在这种情况下。

Should have spent some minutes more thinking. 应该多花几分钟的时间思考。 The problem was that I instantiated the BehaviorSubject object with the type , but didn't declare the variable with it. 问题是我实例化了typeBehaviorSubject object ,但没有用它declare variable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM