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
.