0

I'm just learning flutter - I build a few basic tutorials and am now trying to build a simple notepad application.

https://github.com/sketchbuch/examples_flutter/blob/master/notes/lib/src/components/notepad/notepad.dart

In this file I have a stateful widget which has a state property. What I don't understand is why I need the stateful widget at all... the state class seems to be doing everything including building the list of notes.

Am I doing this right? It doesn't seem right that state builds the whole stateful widget...maybe it is just because my app is basic at the moment but the stateful widget doesn't seem to do anything at the moment.

I just would have thought that the staefulwidget would handle rendering and state would just be an object storing properties

Arnold Rimmer
  • 2,723
  • 2
  • 16
  • 23
  • "widget which has a state property" I don't see a `state` property. Do you mean a `_notes` property? – Günter Zöchbauer Mar 04 '19 at 19:13
  • Even if you do not need the StatefulWidget, Flutter needs it. Reuses the state even when it creates a new widget instance. – Günter Zöchbauer Mar 04 '19 at 19:15
  • yes that is in my state but the stateless widget Notepad has this state so what is "class Notepad extends StatefulWidget" for? Everything including rendering seems to happen in the state "class _NotepadState extends State". Should it be doing this? – Arnold Rimmer Mar 04 '19 at 19:16
  • Rendering happens in Flutter rendering the widget. The widget is recreated for every build and reuses the state if it hasn't changed since the last build. – Günter Zöchbauer Mar 04 '19 at 19:19

2 Answers2

0

As pointless as an empty StatefulWidget subclass looks like, it is necessary. That class is what allows Flutter to manipulate the State subclass.

Without a StatefulWidget, Flutter is unable to create/update/dispose of a State.

StatefulWidget subclass is also a way to customize the behavior of State by adding fields in StatefulWidget and using them in the State class.

Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432
0

You're saying

My Notepad widget as such is not doing anything but my _NotepadState is. Then why should I use the redundant Notepad widget?


Why is it required?

Your Notepad widget is not alone. It has super classes. It extends StatefulWidget which in turn extends Widget (which extends DiagnosticableTree which extends Diagnosticable).

The super classes are doing all the heavy lifting for you and that you had to write almost zero code. Mostly it is used to accept parameters and create state.

Hope that helps!

Amsakanna
  • 12,254
  • 8
  • 46
  • 58