0

I'm using a TabBarView with two tabs that each display a Stateful widget-- Let's call them the Main Widget and the Configuration Widget.

The Configuration Widget serves as a 'configuration section' for the Main Widget-- I use it to set things like the background color and a URL variable in the Main Widget that's used to grab data from a web service.

The Main Widget has a ListView that is populated by that web service. I'm using the Bloc pattern to send events from one widget to the other, which is how the Configuration Widget sends a URL value over to the Main Widget-- an event is fired, which courtesy of the bloc becomes a 'state' object with the new URL in it that the Main Widget can read in its constructor and use to build the ListView.

Right now I use the wantKeepAlive mechanism in the Main Widget to avoid repopulating the ListView if I haven't changed the URL in the Configuration Widget-- if I don't do so the Main Widget rebuilds whenever I return to its tab, even if I haven't changed anything in the Configuration Widget.

What I want to do is have that value set selectively, so that when the URL is changed in the configuration, update the wantKeepAlive value to false so the ListView does rebuild with the new URL. But when it's done building, set the wantKeepAlive value back to true, so no rebuilding is done, until the next URL change.

Is there a lifecycle method I can call when a build() method is completed, where I can set the wantKeepAlive setting? I don't think doing so in the build method itself would work. Open to suggestions, and thanks.

larryq
  • 15,713
  • 38
  • 121
  • 190
  • `"Right now I use the wantKeepAlive mechanism in the Main Widget to avoid repopulating the ListView "` - you mean your bloc performs some heavy jobs when switching between your two tabs? – pskink Jun 23 '19 at 05:54
  • @pskink nothing heavy, just sends out an event when warranted or makes a webservice call. It's not making long calculations or anything along those lines. – larryq Jun 24 '19 at 05:39
  • so whats wrong in repopulating the ListView in that case? widget rebuilding is very cheap in flutter... – pskink Jun 24 '19 at 06:22
  • @pskink I guess I misunderstood what you meant by 'heavy'-- calling the webservice takes a few seconds, and I'd prefer not to have to wait for the list to redraw if I don't have to, if the data is already there. – larryq Jun 24 '19 at 17:46
  • OK the question is why would you want to call your web service each time you switch the tabs - normally only your ListView needs to be rebuilt using the data that is already fetched from the web service - it seems that your bloc pattern is not used correctly – pskink Jun 24 '19 at 17:51
  • That's the thing: I don't want to call the web service whenever I switch tabs, not unless I've changed the URL. The problem for me was figuring out how to tell the Main Widget to change the `wantKeepAlive` property when the URL id changed, so its bloc could make the webservice call. – larryq Jun 24 '19 at 19:00
  • you dont need any `wantKeepAlive` nor trickss like `addPostFrameCallback` - just make sure your bloc does not reload remote data every time you rebuild your `ListView` – pskink Jun 24 '19 at 19:33
  • The TabBarView destroys and reloads widgets when you change tabs, so I need to use the `wantKeepAlive` variable to prevent that. Otherwise my bloc (which is used in the stateful widget constructor) will call the webservice every time I change tabs back, no matter if I need to or not. – larryq Jun 24 '19 at 19:44
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/195482/discussion-between-larryq-and-pskink). – larryq Jun 25 '19 at 03:18

1 Answers1

2

You can use addPostFrameCallback of your WidgetsBinding instance to execute some code after your widget was built.

  _onLayoutDone(_) {
    //add your code here
  }

  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback(_onLayoutDone);
    super.initState();
  }
diegoveloper
  • 93,875
  • 20
  • 236
  • 194