In Flutter, what would be a clear explanation with an example?
My confusion is about key
, as in the code below.
MyHomepage({Key key, this.title}) : super(key: key);
Flutter uses Keys for a lot of things inside the framework. Since your widget extends StatelessWidget or StatefulWidget, this is the way of your widget having a key so that the framework knows what to do with it (the super function is passing the key to the superclass widget - which can be understood by the framework).
The key concept is flutters' way to keep a reference to state and access the state at different times or maintain it while modifying the widget tree. Flutter checks the previous state before re-rendering and if the previous widget is of the same type as the new one — it is going to maintain the old state.
Key class Null safety A Key is an identifier for Widgets, Elements and SemanticsNodes. A new widget will only be used to update an existing element if its key is the same as the key of the current widget associated with the element.
super is used to call the constructor of the base class. So in your example, the constructor of CardTitle is calling the constructor of StatelessWidget .
The code is the constructor of the MyHomepage
widget.
{Key key, this.title}
It declares two optional named parameters (optional named because of {}
) where
the first is of name key
with type Key
the second is of name title
with the type of the field this.title
and automatically initializes this.title
with the passed value. This is nice syntactic sugar that saves some writing.
:
starts the initializer list.
The initializer list allows some to execute some expressions before the call is forwarded to the constructor of the super class.
When a class is initialized, read access to this
is forbidden until the call to the super constructor is completed (until the body of the constructor is executed - in your example the constructor has no body).
The initializer list is often use to validate passed parameter values with assert(key != null)
or to initialize final
fields with calculated values (final
fields can't be initialized or updated later).
super(key: key)
forwards to the constructor of the super class and passes the parameter key
passed to MyHomepage
to the super constructors key
parameter (same as for MyHomepage({Key key})
).
Thanks for Günter's detailed explanation which helped me at the very beginning. Here I would like to explain the background for this question, and especially the punctuation as syntax a little bit.
The mentioned line of code,
MyHomepage({Key key, this.title}) : super(key: key);
should come from the auto-generated Flutter application boilerplate.
The complete context is:
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
For me, the weird punctuation (curly brackets{ }
and two colons :
) is what prevented me from comprehending its syntax.
Curly brackets of {Key key, this.title}
: is the syntax for declaring optional parameters while defining function in Dart.
The first colon of MyHomepage(...) : super(key: key)
is a separator that specifies the initializer list (super(key: key)
) of constructor function MyHomepage(...)
The second colon within super(key: key)
is the way how you pass a parameter to a named function (super()
in this case).
enableFlags
is defined as followsvoid enableFlags({bool bold, bool hidden}) {...}
parameterName
before value
, separated with colon :
, which is safer for developer than the Pythonic way. The counterpart syntax in Swift should be external parameter.
enableFlags(bold: true, hidden: false);
I wish this could help.
All the definitions and examples can be found in Dart's official document.
Flutter uses Keys for a lot of things inside the framework. Since your widget extends StatelessWidget or StatefulWidget, this is the way of your widget having a key so that the framework knows what to do with it (the super function is passing the key to the superclass widget - which can be understood by the framework).
Except for that is only Dart syntax (which is awesome by the way).
You can learn more about Keys here: https://www.youtube.com/watch?v=kn0EOS-ZiIc
Another example is the testing framework. Flutter driver needs a key to know which widget to look for and interact with it. With this syntax your widget will have a key and it will work!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With