Nice Hack for the Classic On-Page-Load vs Post-Page-Load Automatic Actions Catch-22

Best Practices

Ever beat your head against the wall trying to find a way to activate an action (custom or otherwise) automatically when navigating to a page, only to discover that putting an action in on-page-load that has to act on something in the widget tree inside buildcontext can't get ahold of the widget, because it doesn't exist yet?

Our case: For reasons I won't go into here, we turned off all back-navigation in our app, because something deep in the flutterflow framework does naughty things with calling functions on pages that are no longer the page you're on...even to the point of trying to access Firebase after the user logs out...oof. But we navigate the user to a details page and want them to be able to return to the same item in the home list, even if it's a long listview/gridview list.

Getting the scroll location in the list was a bit of custom action coding, but the question was how to invoke it when the user navigates back. Doesn't work. Catch-22. ...unless you want to make the user tap a button on the home screen called "Yeah, I know I shouldn't have to do this, but tap this button to go to the last item viewed."

For this and many other uses we've run into where we need to trigger an action on page load but have the logic persist into the post-load, here's a hack that works:

1) Create a timer and make it invisible on the page.

2) Timers are one thing you can actually trigger from an action in on-page-load. So once you've created the timer on page, choose the start timer action in your topmost widget in your tree on that page (that's your on-page-load actionflow).

3) Set the timer to, say, 1millisecond or 50milliseconds...you choose.

4) Add your actions to the timer's "actionflow" triggered when the countdown is over.

Sure, there's a tiny bit of lag there, but...sure enough....now the user can go to an item in the list and return with navigate-to action and still find themselves back at the correct scroller location.

2