-1

Inside one of my Providers I want to listen to a Stream. For the sake of clarity I simplified the code and Stream by having just int values. In reality this can be a Stream from Firestore snapshots to listen to.

class MyProvider with ChangeNotifier {
  int _data = 0;
  int get data => _data;

  // called when app gets initialized
  void init() {
    // stream which is not updated often (e.g. snapshot listener from Firestore)
    final stream = Stream<int>.periodic(
      const Duration(minutes: 5),
      (count) => count++,
    );

    stream.listen((event) {
      _data = event;
      notifyListeners();
    });
  }
}

Is it ok to listen to a stream inside Provider or is there something I need to consider?

I dont want to use a StreamProvider because I have more business logic in my ChangeNotifierProvider which is not possible with StreamProvider.

Dalon
  • 566
  • 8
  • 26

1 Answers1

0

Consider refactoring the business logic into separate components, then use StreamProvider. If that's not possible, in my opinion, it's not wrong to update a change notifier according to stream data.

Crizant
  • 1,571
  • 2
  • 10
  • 14