9431

What is initState and super.initState in flutter?

Question:

In the documentation it is written but I am not able to understand.

<blockquote>

Called when this object is inserted into the tree.

The framework will call this method exactly once for each State object it creates.

Override this method to perform initialization that depends on the location at which this object was inserted into the tree (i.e., context) or on the widget used to configure this object (i.e., widget).

If a State's build method depends on an object that can itself change state, for example a ChangeNotifier or Stream, or some other object to which one can subscribe to receive notifications, then the State should subscribe to that object during initState, unsubscribe from the old object and subscribe to the new object when it changes in didUpdateWidget, and then unsubscribe from the object in dispose.

You cannot use BuildContext.inheritFromWidgetOfExactType from this method. However, didChangeDependencies will be called immediately following this method, and BuildContext.inheritFromWidgetOfExactType can be used there.

If you override this, make sure your method starts with a call to super.initState().

</blockquote>

But I'm not sure about its meaning. Can you explain it?

Answer1:

Credit to @Remi, initState() is a method which is called once when the stateful widget is inserted in the widget tree.

We generally override this method if we need to do some sort of initialisation work like registering a listener because unlike build() this method is called once.

And to unregister your listener (or doing some post work), you override dispose()method.

Recommend

  • JXA Move a message to a different folder/mailbox
  • jQuery UI .sortable() call is slow when applies to thousands of elements
  • IE8 stops network access after 5 long polling request
  • missing pie chart and other elements when display serenity report from jenkins
  • CakePHP 3: change order in dateWidget
  • SIP Makefile fail (gnuwin and mingw)
  • How to generate an asynchronous reset verilog always blocks with chisel
  • Yii2: Using Kartik Depdrop Widget?
  • Initialization section of the package
  • How do you keep a running instance for Google App Engine
  • How to change placeholder text in an autocomplete activity of android google place?
  • MRO with multiple inheritance in python
  • SIP API media codecs
  • Configure Spring's MappingJacksonHttpMessageConverter
  • Two Tables Serving as one Model in Rails
  • How do I retrieve the user information of a user authenticated with Apache's mod_ldap?
  • JPA flush vs commit
  • Elasticsearch script query involving root and nested values
  • Custom preprocessing in caret
  • Why use database factory in asp.net mvc?
  • JqueryMobile Popup menu is not working
  • Intel-64 and ia32 atomic operations acquire-release semantics and GCC 5+
  • Zoom in and out of jPanel
  • How do I configure context broker accept post requests from my remote sensor?
  • Firefox Extension - Monitor refresh and change of tab
  • Unable to get column index with table.getColumn method using custom table Model
  • Saving Changes After In-App Purchase Has Been Purchased
  • OOP Javascript - Is “get property” method necessary?
  • Android - Material Design - NavigationView - How to put vertical scroll?
  • Transactional Create with Validation in ServiceStack Redis Client
  • Hardware Accelerated Image Scaling in windows using C++
  • Illegal mix of collations for operation for date/time comparison
  • swift auto completion not working in Xcode6-Beta
  • Javascript Callbacks with Object constructor
  • To display the title for the current loaction in map in iphone
  • ORA-29908: missing primary invocation for ancillary operator
  • Cannot Parse HTML Data Using Android / JSOUP
  • Suggestions to manage Login/Logout transitions
  • How do you join a server to an Active Directory (domain)?
  • How to get NHibernate ISession to cache entity not retrieved by primary key