I'm still quite new to Flutter and I use `flutter_bloc` for state management.
In bloc I'm listening to a repository method `Stream<List<UserAlert>> alerts()`. In this method I listen both to `.onChildAdded` and `.onChildRemoved` events on a Firebase real time database node and update a local `List<UserAlert> alerts`, but I'm not succeeding in returning it once it updates so I get null value on bloc listener. `NoSuchMethodError: The method 'listen' was called on null.` I chose to manually manage it as I don't want to download the whole node at every change as it happen when listening to `.onValue`. I tried `return alerts as Stream<List<UserAlert>>` inside both firebase snapshot scopes but I get a type cast error.
I tried `Stream.value(alerts)` both inside each scope and as the very last thing in the method but still returns null.
How can I get `alerts` to be returned as a stream?
Thank you very much for your help.

Repository method:

```
Stream<List<UserAlert>> alerts() {
    print('alerts() called');
    Stream<List<UserAlert>> alertStream;

    List<UserAlert> alerts = [];
    // Option 2  return a list manually updated by two different events: More complicated but very efficient(bandwidth), lowest data consuming = Lowest Firebase bill.
    // returns one event per node record at app start and only new afterwards

    _alertSubscription = _databaseReference.onChildAdded.listen((Event event) {
      print(' observer .childAdded: added alert is : ${event.snapshot.value}');
      UserAlert userAlert = UserAlert.fromSnapshot(event.snapshot);
      alerts.add(userAlert);
//      alertStream = Stream.value(alerts); // alerts as Stream<List<UserAlert>>;
//      return alerts;

      print(
          'observer .childAdded: alerts are: $alerts and total alerts are ${alerts.length}');
    });

    // returns one event per node removed record
    _alertSubscription =
        _databaseReference.onChildRemoved.listen((Event event) {
      print(
          'observer .childRemoved: removed alert is : ${event.snapshot.value}');
      int index = alerts.indexWhere(
          (userAlert) => userAlert.id.startsWith(event.snapshot.value['Id']));

      print('index to remove is: $index');
      alerts.removeAt(index);

//      return alerts;
      print(
          'observer .childRemoved: alerts after removing are: $alerts and total alerts are ${alerts.length}');
//      return alerts;
    });
//    yield* alertStream;
    Stream.value(alerts);
  }

```

Bloc listener:

```
Stream<AlertState> _mapLoadAlertToState() async* {
    _alertSubscription?.cancel();
    _alertSubscription = _alertRepository
        .alerts()
        .listen((alerts) => add(AlertsUpdated(alerts)));
////        (List<UserAlert> alerts) {
////      print('_mapLoadAlertToState() userAlterts are: $alerts');
////      add(AlertsUpdated(alerts));
//    });
  }

```