akshu patel
akshu patel

Reputation: 11

In Flutter why do we need use the createState() even inside StatefulWidgets()?

Example code from Flutter.dev docs. Why do we need to override the CreateState() here.

class Counter extends StatefulWidget {

  @override
  *_CounterState createState() => _CounterState();*
}

class _CounterState extends State<Counter> {
  int _counter = 0;

  void _increment() {
    setState(() {
      _counter++;
    });
  }

Upvotes: 0

Views: 711

Answers (1)

Ashiq Ullah
Ashiq Ullah

Reputation: 137

It is very important to know the life cycle of the StatefulWidget. The createState() actually returns the instance of the State subclass. In simple words, it triggers the subclass of the State(in your case it is _CounterState). If you do not override it, the below class _CounterState will be declared as an unused element.

Upvotes: 1

Related Questions