Search
 
SCRIPT & CODE EXAMPLE
 

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.
Comment

PREVIOUS NEXT
Code Example
Dart :: flutter row vertical direction 
Dart :: how to use $ input in dart as a string 
Dart :: how to define format snippet of class name as file name in dart : flutter 
Dart :: flutter appbar is still grey 
Dart :: flutter navigator get result 
Dart :: check if animation complete in flutter 
Dart :: Flutter Text new fline 
Dart :: dart zip two lists 
Dart :: how to groupby list of maps in flutter 
Dart :: flutter standarrt icon size 
Dart :: dart break double for loop 
Dart :: dart destructor 
Dart :: const issue on new flutter version 
Dart :: dart map what is 
Dart :: dart uzunlikni olish 
Swift :: swift text align center 
Swift :: toggle in swift 
Swift :: delete padding list swiftui 
Swift :: how to swift pie chart quartzcore framework 
Swift :: swift http request 
Swift :: white or light ASAuthorizationAppleIDButton “Sign in with Apple” button - Swift 
Swift :: fetch codable from userdefaults ios swift 
Swift :: how to disable uitableview scrolling in swift 
Swift :: swift dispatch queue 
Swift :: swift scrollview scroll to bottom 
Swift :: swift navigation bar title color 
Swift :: swift arrays 
Swift :: swift 5 make image fit uiimageview 
Swift :: swift array remove 
Swift :: reprobate 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =