Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR DART

is init state executed when returning with navigator flutter

// Have a look at the source page.

Hi Liz,

I am no longer actively doing flutter development, however I will do my best to clarify.

When using Flutter Navigator.push to change from page A to page B, I had previously expected page A to be disposed and page B to be initialized (initState called). This would allow me to perform clean up on page A (close any connections, streams etc.) and to initialize state for page B (open connections, streams etc.). The misconception here is that page A is not disposed. It is kept in the navigator history/stack to facilitate returning via Navigator.pop. Interestingly deactivate is called on page A.

When returning to page A via Navigator.pop page B is disposed of (fully removed from the tree), so clean up can be performed (via overriding the dispose method). However initState is not called on page A. My understanding is that page A was not considered removed from the tree due to being part of the Navigator stack (it may have even still been visible - e.g a popup dialog).

I have later found that an effective method of handling state when returning to a page via the Navigator can be achieved by utilizing the future that is returned from the Navigator.push methods. This future will be executed when the navigator pops back to this page, and can contain an optional payload which is populated by the caller of Navigator.pop.

I suggest having a look at the documentation for Navigator. https://api.flutter.dev/flutter/widgets/Navigator-class.html. There is a small section named "Routes can return a value" which gives an example of how to get the value returned from Navigator.push. If the value is not of interest, then you could instead perform some action in the future to effectively perform the desired state initialization.
Source by github.com #
 
PREVIOUS NEXT
Tagged: #init #state #executed #returning #navigator #flutter
ADD COMMENT
Topic
Name
5+4 =