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



考虑以下代码(不要介意无用的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,在这种情况下,我用data参数传递给BehaviorSubject(在每个新的listen上发送最后一个eventStreamController(?当我将包含model信息的object作为fields传递给BehaviorSubject时,这将允许我有方便的代码建议,就像在这种情况下一样。

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

最新更新