Hi Felix, first off thanks for all your work and general contributions.
This isn't an issue its a question and I hope its okay that I'm asking here. It appears this may be similar to what munkhp asked about multiple blocs of the same type.
I've implemented a modified version of your Timer tutorial onto a couple new test pages of my timer app I'm building. I was hoping I could ditch the timers and stopwatches I'm passing around everywhere for just using Stream.periodic and BlocBuilders. My issue however is that my app is a multi-timer, with the user able to add as many timers as they want that all operate and display independently.
Is it possible to have multiple instances of the same TimerBloc type with different Text widgets tracking different streams? I currently have different widgets following different Timers and Stopwatches via passing them around in constructors and using SetState to update the Text widgets. That works fine and they track between the timer list page and the enlarged single timer page. It would be great if I could get bloc to run the timers then it'd be way easier to implement call backs and other features etc...but if it would only work with one timer at a time then I won't be able to use it the way I was hoping. I'm stuck on getting more than one going at the same time, and figured I should confirm whether this is even possible or not with Bloc.
Either way, thanks again for all your work and excellent documentation. As a noob developer, as soon as I started to understand how Bloc works(mostly thanks to your docs and ResoCoder), I've made a lot of progress in a short amount of time in terms of implementing features I previously didn't know how to do.
Hi @TheDudeAbides1984 👋
You can use multiple blocs of the same type as long as they are siblings.
Not sure exactly what sort of UI you're after, but you could have a Column /Row which contains multiple BlocProvider<TimerBloc> and use each bloc state to generate a Text widget.
Let me know if you need further clarification. ✌
Hi @RollyPeres ,
Thanks for the quick response! Oh okay so instead of having one BlocProvider<TimerBloc> as the ancestor of my material app, I bring them way down in the widget tree and use a separate one for each stream, is that correct?
Would you mind expanding on what the the limitation is of them having to share a parent widget? I ask because currently my app has a 'RowTimerWidget' for the timer list page, and a CircleTimerWidget for the single timer page and pressing a RowTimerWidget takes you to the corresponding larger single view of that timer. Both are initialized from a 3rd user input page. When you say 'as long as they are siblings', would that provide any limitations to that functionality?
Hi @TheDudeAbides1984 the main things to keep in mind when providing blocs are:
This means that you should generally provide blocs as low in the widget tree as possible (scope them to only the widgets that need access) and avoid providing multiple blocs of the same type to the same widget tree (unless you want to intentionally override blocs provided higher up).
Let me know if that helps 👍
I bring them way down in the widget tree and use a separate one for each stream, is that correct?
You would indeed use a separate bloc driving each text as a result of an internal ticker stream.
When you say 'as long as they are siblings', would that provide any limitations to that functionality?
No, you can still keep your functionality.
Hey @felangel, thanks for the response, very helpful! I've learned a lot the last few days between your guys' responses and diving into the Bloc Access portion of the docs. Moving the bloc providers down has been a bit of a struggle because I keep running into the 'BlocProvider.of() called with a context that does not contain a bloc....'
But there is plenty to read about that on here and Stack Overflow so I'll keep chipping away at it and I'll get to where I need to be especially once I have a better understanding of context.
@RollyPeres thanks for confirming I can do what I want to in this case with bloc. You 2 have been most helpful.
@TheDudeAbides1984 that's great to hear! I'm closing this issue for now but feel free to comment with additional questions and I'm happy to continue the conversation 👍
Most helpful comment
Hey @felangel, thanks for the response, very helpful! I've learned a lot the last few days between your guys' responses and diving into the Bloc Access portion of the docs. Moving the bloc providers down has been a bit of a struggle because I keep running into the 'BlocProvider.of() called with a context that does not contain a bloc....'
But there is plenty to read about that on here and Stack Overflow so I'll keep chipping away at it and I'll get to where I need to be especially once I have a better understanding of context.
@RollyPeres thanks for confirming I can do what I want to in this case with bloc. You 2 have been most helpful.