Reputation: 1024
I am new to flutter and I am using the Cubit from flutter_bloc package. I have this situation going here.
I have screen-A as in diagram below where I show list of videos and the likes, comments connected with this video.
Now I have screen-B (some kind of detail page) where I have a button that launches screen-C (which is duplicate screen of screen-A) that plays only one video at a time (not scrollable).
So screen-A, screen-B, screen-C are actually different routes and I push these routes as I navigate..
So let's assume:
Video1.mp4 is playing right now on Screen and me as a user has not liked or commented on this video.
Then I open Screen B. Tap on a button that opens the same video1.mp4 on screen C.
Then I tap the like button to add my 'like' on this video.
Now I navigate back from Screen C -> Screen B -> Screen A.
Since the same video is playing here, I want the 'like count' to be updated with +1 count and the button has to be in 'liked' state.
The problem is I have no idea how to update this state from another screen.
Upvotes: 0
Views: 1430
Reputation: 1
Since you want to change the state of A from C you can just pass the context(let it be "parentContextA") of screen A to screen C and when you use the like functionality on page C there must be an update API which updates the like count on your database or local storage, now if that update is successful just call the call the api of fetching the videos in screen using parentContextA,
e.g : parentContextA.read().fetchAllVideos() or BlocProvider.of(parentContextA).fetchAllVideo(),
Upvotes: 0
Reputation: 116
In this situation, I will provide the Cubit
or the Bloc
at the top of the widget tree (above the MaterialApp
) and use it in screens A and C to change the state. Thus, both screens will listen to the changes and be updated with the BlocBuilder
.
Upvotes: 3