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

402 Views Asked by At

Consider this code (don't mind the useless listen method, it's just to show the use case):

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? 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.

1

There are 1 best solutions below

0
On

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.