Using a stream viewmodel with stream that requires arguments #1054
-
Hello, I've been using stacked for a while and I love it! I've build a really great application and compared to other framework this one is IMO the best by far. Now I got stack a little bit with a stream and I am not sure how to resolve it. I built viewmodel before |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @zuboje good question, I can see you've been going deep in Stacked if you have a question like this 😄 To solve this you simply change the So we have a function called So in your case, you would start the ViewModel with an Empty Stream, that does nothing. When you have the data you need to create the Stream you want, you can set that to the source, and then call late Stream<int> _currentSource; // <== The stream we'll use to change sources
StreamCounterViewModel() {
_setSource();
}
void _setSource() {
_currentSource = isSlowEpochNumbers // <==== Change the source to what we want it to be
? exampleLocator<EpochService>().epochUpdatesNumbers()
: exampleLocator<EpochService>().epochUpdateNumbersQuickly();
}
@override
Stream<int> get stream => _currentSource; <== Instead of using the Stream directly, use the variable
void changeStreamSources() {
isSlowEpochNumbers = !isSlowEpochNumbers;
_setSource();
notifySourceChanged(); // <== After source has been changed we notify Stacked about it
} Stacked will manage the stream for you by unsubscribing, disposing, then resubscribing and running everything as usual with the new Stream. Goodluck 🤜 |
Beta Was this translation helpful? Give feedback.
Hi @zuboje good question, I can see you've been going deep in Stacked if you have a question like this 😄
To solve this you simply change the
Stream
in the ViewModel, then rebuild it.So we have a function called
notifySourceChanged
that can be called when a source has changed. What this will allow you to do is start change the stream to a new stream with the same data type dynamically.So in your case, you would start the ViewModel with an Empty Stream, that does nothing.
When you have the data you need to create the Stream you want, you can set that to the source, and then call
notifySourceChanged
. Here's a small example, I'll comment on it.