Home > front end >  why do we need to extend statefull or stateless classes rather create an object of them and use it i
why do we need to extend statefull or stateless classes rather create an object of them and use it i

Time:04-23

I am new to flutter, I wonder why we extend Statefull or stateless classes to create our own widgets, why not creating the objects of the instead. Now many others might say it has build function that needs to be overridden but I guess it can also be done inside the object. Please give me an explanation.

CodePudding user response:

flutter has lifecycle, widget will appear after build https://i.stack.imgur.com/94idE.png

CodePudding user response:

Both Stateful and Stateless classes are abstracts.

So that, to use methods such as initState in Stateful or "build" in both kind of widgets you need extend either Stateful or Stateless (exactly "extends" keyword, if you use "implements" keyword, you will be forced to implement all of the methods described in abstract).

More about:

  1. StatefulWidget
  2. StatelessWidget
  • Related