1

I am working on a web app where I need to build a widget depending on a state. Now, I have different Cubits and they have different states. One is main Cubit, that is on the top widget or page. Then I have another cubit state that builds part of that page. Now, what happens is that once I send a state to build the widget, it works fine but that state exists. Now, as state exits therefore changing the top state rebuilds that widget again. Therefore, duplicate widgets are created. I do not know, how to make state non existing once action is completed. I tried to send completed state that is different but same issue exits if I try to build widget again. As code is complex and long but I am writing code snippet below for understanding.

BlocBuilder<TimeCubit, TimeState>(builder: (context, state) {
   return FutureBuilder<Object>(
      future: ..
      builder: (context, AsyncSnapshot<Object> snapshot){
             return widget;
              
            BlocBuilder<AnotherCubit, AnotherState>(builder: (context, state) {

                         //This state exists even if page rebuilds itself from above 
  state. I tried to send completed state  so that it will not rebuilt but now after 
 rebuilding top level state but it still duplicate the widget.

                        if(state is anyState){

                         return FutureBuilder<Object>(
                         future: ..
                         builder: (context, AsyncSnapshot<Object> snapshot){
                                                                                                         
                         context.read<AnotherCubit>().actionCompleted(true);

                         return anotherwidget;
                     }
                    );
                    }
                }
            }
);
}
WasimSafdar
  • 1,044
  • 3
  • 16
  • 39

1 Answers1

0

you can easily have a listener that allows you to listen to the states, depending on the state, then modify the widget or change the one you want

  • Listener do not build a widget in response to state. It just perform specific actions and then builds a widget. – WasimSafdar Nov 10 '22 at 14:18