What is this widget property from the StatefulWidget flutter
Solution 1
The MyApp
class extends StatefulWidget, which means this widget stores mutable state. When the MyApp widget is first inserted into the tree, the framework calls the createState()
function to create a fresh instance of _MyAppState
to associate with that location in the tree. (Notice that subclasses of State are typically named with leading underscores to indicate that they are private implementation details.) When this widget’s parent rebuilds, the parent creates a new instance of MyApp
, but the framework reuses the _MyAppState
instance that is already in the tree rather than calling createState again.
To access the properties of the current MyApp
, the _MyAppState
can use its widget property
. If the parent rebuilds and creates a new MyApp
, the _MyAppState
rebuilds with the new widget value. If you wish to be notified when the widget property changes, override the didUpdateWidget()
function, which is passed as oldWidget to let you compare the old widget with the current widget.
Now as Per Docs: widget property
This property is initialized by the framework before calling initState. If the parent updates this location in the tree to a new widget with the same runtimeType and Widget.key as the current configuration, the framework will update this property to refer to the new widget and then call didUpdateWidget, passing the old configuration as an argument.
Solution 2
Long answer short
You have extended State
class
Referring docs
The State
class has a readonly
property called widget
. Which is what you are referencing to.
Rablidad
Updated on November 20, 2022Comments
-
Rablidad over 1 year
Its a very simple question, I sometimes see something like:
widget.title
orwidget.(anything)
in flutter; like this example in the Text widget child ofAppBar
Widget:class MyApp extends StatefulWidget{ // some declarations here @override _MyApp createState() => _MyApp(); } class _MyApp extends State<MyApp>{ // some declaration here @override Widget build(BuildContext context){ return MaterialApp( home: Scaffold( appBar: AppBar(child: Text(widget.title),), ), ); } }
what does is this actually?
widget.title
i mean, what is thewidget
referencing? what is it?