What is Flutter and Use of Stateful Widget in Flutter.





Google's Flutter is an open-source UI (User Interface) framework. It enables the creation of cross-platform mobile, web, and desktop applications from a single codebase. Here's a basic overview of Flutter:


Dart Programming Language: Dart is the programming language used by Flutter. Dart is a modern, object-oriented programming language with features such as a just-in-time (JIT) compiler that allows for rapid development and efficient performance.


Widgets: Flutter is built on a reactive and component-based design, and your UI is built with widgets. Widgets are the foundation of a Flutter application and can be used to create elaborate user interfaces. Flutter has a large number of basic and specialized widgets.


Flutter's hot reload capabilities is one of its standout characteristics. It enables you to see changes to your code immediately reflected in the app without having to restart it. This greatly accelerates the development process.


Cross-platform Development: Flutter allows you to create apps for many platforms such as Android, iOS, web, and desktop. You may target several platforms with a single codebase, minimizing development time and effort.


Material Design and Cupertino: Flutter comes with two pre-designed widget collections: Material Design and Cupertino. Material Design widgets adhere to Google's design guidelines, resulting in a modern and visually appealing user interface. Cupertino widgets mimic the iOS native look and feel, making it easy to create apps that look like they're from iOS.


Access to Native Device capabilities: Flutter allows you to use plugins to gain access to native device capabilities and APIs. It has a broad ecosystem of plugins that cover a wide range of functions, including camera access, location services, and network requests.


Performance: Flutter apps are well-known for their high performance. Flutter creates its own widgets using a rendering engine, resulting in fluid and responsive user interfaces. Furthermore, the Dart programming language and Flutter framework optimize performance through the use of just-in-time (JIT) and ahead-of-time (AOT) compilation techniques.


Community and Support: Flutter has an active and lively community. There are several tools, courses, packages, and sample code available online. The community actively contributes to the framework's progress and offers assistance through forums, Stack Overflow, and other channels.



What is Flutter Stateful Widget and it's Lifecycle?


A state object is created by Flutter. This object contains all of the mutable state for the widget. Two things characterize the concept of state: -

1. The widget's data source may change.
2. Data cannot be read synchronously when the widget is formed (all state must be established by the time the build method is invoked).

The following basic steps comprise the lifecycle: -

  • createState()
  • mounted == true
  • initState()
  • didChangeDependencies()
  • build()
  • didUpdateWidget()
  • setState()
  • deactivate()
  • dispose()
  • mounted == false


Why are Stateful Widget and State classes distinct?

In a nutshell: performance.


The short version is that 'state' objects live a long time, but stateful widgets are discarded and rebuilt anytime the configuration changes. Rebuilding a changeable widget is incredibly cheap for Flutter.


It is capable of constantly constructing its widget in response to data changes and as needed.



1. createState() 

When flutter is told to create a stateful widget, it immediately runs createState().


This method must exist. A stateful widget rarely has to be more sophisticated than this. Here's an example:


class HomeScreen extends StatefulWidget {

@override

HomeScreenState createState() => HomeScreenState();

 


2. mounted is true

The state class is created by createState, and it is given a buildContext

The place in the widget is placed in an unnecessarily simplistic BuildContext. Here's a more detailed explanation. 

The attribute 'bool this.mounted' is shared by all widgets. When the buildContext is assigned, it becomes true. When a widget is unmounted, calling setState is an error.


3. initState()

When the widget is built, this is the first method that is called. 

initState is called only once. It also has to call super.initState().

This @override technique is ideal for:


1. Initialize data for the created instance of the widget that is dependent on the given BuildContext.

2. Set properties that are dependent on this widget's 'parent' in the tree.

3. Subscribe to Streams, ChangeNotifiers, or any other object that has the potential to update the data on this widget.


@override

void initState() {

super.initState();

}


4. didChangeDependencies() 

On the first time the widget is constructed, the didChangeDependencies method is called immediately after initState.


It will also be invoked anytime an object on which this widget relies for data is called. For example, if it is dependent on an InheritedWidget that updates.


Because build is always called after didChangeDependencies, this is rarely necessary. However, this is the first change you must make before calling BuildContext.inheritFromWidgetOfExactType. This would essentially make this State 'listen' to updates on a Widget from which it is inheriting data.


It may also be beneficial if you need to perform network calls (or any other expensive operation) when an InheritedWidget updates, according to the documentation.


5. build()

This technique is frequently invoked (consider fps + render). It is a mandatory @override that must return a Widget.

Remember that in Flutter, every gui is a widget with a child or children, even 'Padding' and 'Center'.


6. didUpdateWidget(Widget oldWidget)

didUpdateWidget() is called when the parent widget changes and wants to rebuild this widget (because it needs to provide it different data), but it is rebuilt with the same runtimeType.

This is due to Flutter reusing the long-lived state. In this scenario, re-initializing certain data, as in initState(), is required.

If the state's build() method relies on a Stream or other object that can change, unsubscribe from the old object and re-subscribe to the new one in didUpdateWidget().

This method is essentially a replacement for 'initState()' if the Widget associated with the widget's state nrrds is expected to be rebuilt!

Flutter always called build() after this, thus any future calls to setState are unnecessary.

@override
void didUpdateWidget(Widget oldWidget) {
if (oldWidget.importantProperty != widget.importantProperty) {
init();
}
}

7. setState()

The 'setState()' method is frequently used by both the Flutter framework and the developer.

It is used to tell the framework that "data has changed" and that the widget in this build context needs to be rebuilt.

setState() requires a callback that can't be async. Because repainting is inexpensive, it can be called as often as needed

void updateProfile(String name) {
setState(() => this.name = name;
}

 8. deactivate()

This is rarely used.

'deactivate()' is called when State is removed from the tree, but it may be reinserted before the current frame change is completed. This method exists primarily because State objects can be relocated from one place in a tree to another.


9. dispose()

When the State object is permanently eliminated, 'dispose()' is called.
This is where you unsubscribe and cancel all animations, streams, and so on.


10. mounted is false

The state object can never be remounted, thus calling setState() results in an error.

Comments