Biswas Khayargoli
Biswas Khayargoli

Reputation: 1024

How to alter state of one screen from another screen using Cubit in Flutter?

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:

The problem is I have no idea how to update this state from another screen.

enter image description here

Upvotes: 0

Views: 1430

Answers (2)

Deepraj Das
Deepraj Das

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(),


  1. this must be called in screen C itself
  2. Its better from making the whole bloc or cubit as global since most pages may not use it
  3. The editor seems to remove the cubit name from the code section which has angle brackets present

Upvotes: 0

A7MED_SI
A7MED_SI

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

Related Questions