0

I am struggling with bloc and search delegate and looking for advice (or an example).

I want to be able to trigger a snackbar in the search delegate, go back to the homepage and still be able to press the action button of the search delegate (even though I am now on a different page). To use a chat app as an example: Imagine you delete a chat you have found in the search view, then return to your homepage and, realizing you deleted the wrong chat, press the undo button in the snackbar and the chat (that you deleted in the search view) appears again (this time on the homepage).

If I have a homepage (from which you can access the search delegate) and a search delegate that both hook into the same bloc (and with it a datastream) I have found two ways to do it.

1: I can point both pages towards a new class (call it "data view") in which the bloc provider is initialized, the bloc listeners checks for mistakes and the bloc builder provides the display data. Here I encounter the following problem: If my bloc listener triggers a snackbar in the search delegate view and I then return to the homepage the snapbar is still there (rightly so), but when I press the action button on the snackbar I get a context error.

2: I push the bloc provider one level up and have it initializes before the hompage (and with it the search delegate button) is created. Here I encounter the following problem: The bloc builder in the search delegate cannot find it's bloc provider.

Unfortunately, at this point I cannot provide sample code. Any help is appreciated!

  • I'd go with your second option. You just need to also have the BlocProvider higher up in your Widget tree. The BlocProvider or even a MultiBlocProvider could have your App as its child. – John Weidner Aug 20 '23 at 20:03
  • @JohnWeidner Can't believe it was that simple. Awesome mate! Mate thanks a lot! – julianniedermaier Aug 22 '23 at 05:41

0 Answers0