top of page

[Flutter/dart]Reflect the change of Store even after transitioning with Navigator


Overview


When managing the state with redux, you can use StoreProvider to reflect the change of store in widget. However, the rebuild after changing the store does not reach another page that was reached by Navigator by tapping a button. In this situation, we will explain how to rebuild the transition destination page as well.



Method


Suppose you want to jump to a page that takes a ViewModel (widget.model) that cuts out the store in the onTap event as shown below. In this case, even if this widget is rebuilt, SomePage will not be rebuilt, so even if you change the contents of the store with SomePage, for example, the change will not be reflected in the SomePage widget.


onTap: ()async{
  Navigator.of(context).push(
    MaterialPageRoute(builder: (context1)
        => SomePage(model: widget.model), //Page using ViewModel
    )
  )
}

What to do is bring another StoreProvider to this location.


onTap: ()async{
  Navigator.of(context).push(MaterialPageRoute(builder: (context1)=>
      StoreProvider(
        store: StoreProvider.of<AppState>(context),
        child: StoreConnector<AppState, ViewModel>(
          distinct: true,
          converter: (store)=>ViewModel.create(store),
          builder: (BuildContext context1, ViewModel model)=>
            SomePage(model: model),
        )
      )
    )
  );
},

AppState is the class that represents the state, and ViewModel is the class that cuts out the state of the store (create () is the constructor).


Be careful not to confuse the "context" of the argument of Navigator.of() with the "context" of the argument of the builder of MaterialPageRoute. You get an error like No StoreProvider <AppState> found.



Precaution


However, this method does not seem to guarantee the order in which the two StoreProviders will be rebuilt. (There is no source. The result of trying.)



Lastly


Fundamentally, it may not be good as the structure of the application that the contents of the store affect the transition destination. I think it's best if you can change the structure.

Recent Posts

See All

[Flutter/Dart] Format string with TextField

What want to do I want to create an input form using TextField. For example, if the input content is a monetary amount, I would like to display it in 3-digit delimiters with a ¥ prefix. Rather than ha

Let's do our best with our partner:​ ChatReminder

iphone6.5p2.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Let's do our best with our partner:​ ChatReminder

納品:iPhone6.5①.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Theme diary: Decide the theme and record for each genre

It is a diary application that allows you to post and record with themes and sub-themes for each genre.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Inquiries: Please contact us on Twitter

  • Twitter
bottom of page