This question already has answers here:
What are Keys in the Stateless widgets class?
(5 answers)
Closed 3 years ago.
what does the super and key words do in a dart class?
one example is the code below:
class CardTitle extends StatelessWidget {
final String title;
const CardTitle(this.title, **{Key key}**) : **super(key: key)**;
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.
Key is a type used in Flutter to identify widgets and allows Flutter to know when a widget that's moved in the tree is the same as a widget that was previously in a different location. There's a good video about keys here: https://www.youtube.com/watch?v=kn0EOS-ZiIc
Keys are used as an identifier for Widgets, Elements and SemanticsNodes.You don't need to use Keys most of the time, the framework handles it for you and uses them internally to differentiate between widgets. For more on them see:
https://flutter.dev/docs/development/ui/widgets-intro#keys
As for the Super keyword:
We see in your example the CardTitle widget extends the super class statelesswidget and in its constructor the ":" starts the "initializer list", a comma separated list of expressions executed before the constructors of the super classes and therefore also before the constructors body.
In the example in your question the key parameter passed to the constructor is forwarded to the named parameter key of the unnamed constructor of the super class.
Related
This question already has answers here:
The difference between the use of constructor " className() and className._()
(2 answers)
Closed 1 year ago.
I'm still getting used to Dart and I've searched for an answer to this, but I haven't found one. What does the second line, 'ClassA._();', of the class definition do? The list declaration is only shown to give the class a reason for being.
class ClassA {
ClassA._();
static final List<String> someList = ["A", "B", "C"];
}
If it's a constructor, how would it be invoked?
After doing some more sleuthing I see it creates a singleton. But when is the singleton created? Is there a way to get in front of the instantiation and make some mods?
Thanks in advance for any help given.
ClassA._();
Is a named constructor with the name _. In dart, identifiers that start with underscore (_) are only visible inside the library it is contained in.
The reason you would define a class this way, is that you want to prevent people from creating instances of this class.
I'm trying understand what's the mean of this two final lines of code, on colons... It's a syntax question.
I'm following this github example and I have this doubt on my head.
Can someone help me with this?.
class DietPlan extends ParseObject implements ParseCloneable {
DietPlan() : super(keyDietPlan);
DietPlan.clone() : this();
The part after : is called "initializer list.
It is a list of expressions that can access constructor parameters and can assign to instance fields, even final instance fields.
The first colon, i.e. DietPlan() : super(keyDietPlan); means that you are calling the super constructor, constructor of ParseCloneable in this case.
This is a core OOP concept, you can extend or implement one class to another and you must call the superclass constructor if you do so. This is just a style of doing the same in Dart.
The second colon works in similar way, to understand that you need to understand what is cloning of objects,
Object cloning refers to creation of exact copy of an object. It creates a new instance of the class of current object and initializes all its fields with exactly the contents of the corresponding fields of this object.
This is whats happening on the second line.
I am starting to learn Dart and I can see that a final variable, contrary to many other languages, can't be assigned a value inside the constructor body itself.
Why Dart imposes this limitation?
The initialization of final fields needs to be completed before the constructor body is executed, because in the constructor body read access to this (and its members) is allowed.
Otherwise it would be hard to make guarantees about when it is safe to access final fields.
In other languages where in the constructor body write access to final fields is allowed, it is possible to write object initialization code with unexpected behavior, for example where a final field is read before it was initialized. Dart protects you from doing that.
Where should one keep final values?
In the StatefulWidget (my subclass of course) instance and access it from the State (subclass) via widget.thatFinalField, or
In the State itself. I've seen both approaches already.. Any pros and cons for each of them?
You should store final member fields (which are passed via constructor arguments) on the StatefulWidget and make them public.
The StatefulWidget's associated State should use only the default constructor (no arguments), and its member fields should be private (starting with a _) and mutable. Initialize them inline or in initState if expensive or async work is necessary.
This pattern allows the StatefulWidget to be recreated/rebuilt with new constructor arguments when its parents call setState, while re-using the previous State and letting it keep the values stored in its mutable member fields.
I wanted to write a simple behavior in Dart to be used by a custom element.
#behavior
abstract class AlignmentBehavior implements PolymerBase {
#Property()
bool alignTop = false;
// more properties ...
ready() {
updateAlignment();
}
#reflectable
updateAlignment([_,__]) {
// reference to the element the Behavior is attached to.
// 1) Is that the correct way?
var ele = Polymer.dom(root);
// We need to inherit from PolymerBase to access set(...)
// 2) Is that the correct way?
set('alignTop', aTop);
// .. to more stuff
}
}
My first two questions are already written in the code. How do I access the element the behavior is attached to? What's the proper way of doing this? I currently use Polymer.dom(root) but I don't know if that even works since I have some runtime errors which I am going to explain later in this post. What is the official way of accessing the underlying element? Is it using the JsObject? Is it calling a Behavior function from the parent PolymerElement and pass this, or should you not access it at all?
Another question is whether I have to inherit from PolymerBase or not. The Behavior example at the Github wiki doesn't do so, but in order to access methods such as set to modify a #Property I have to inherit from it. What's the proper way of doing so?
My last two questions are about errors I get. One error asks me to implement getters and setters for my properties, such as adding a getter and setter for alignTop.
And last but not least, I cannot invoke updateAlignment() from my custom element. It says Class 'MainApp' has no instance method 'updateAlignment'.
1)
var ele = Polymer.dom(root);
If you want to access the DOM of the element, this fine.
Just root gives you the same AFAIK.
If you want to access the elements class instance, there is nothing to do. It's this but that is implicit in Dart anyway.
You can only access what is known in the mixin. To make "things" known to the mixin you can create an interface class.
abstract class MyComponentInterface {
void someFunction();
int someField;
String get someValue;
set someValue(String value);
...
}
Then implement the interface in the mixin and the element class and you have a shared contract.
abstract class AlignmentBehavior implements MyComponentInterface, PolymerBase {
The mixin can now access the members because the implementsMyComponentInterface` claims they will exist and
class MyComponent extends PolymerElement with AlignmentBehavior {
will force you to implement it to fulfill the contract of the mixin.
2) looks fine
3)
Another question is whether I have to inherit from PolymerBase or not.
Is basically the same as 1) Any Polymer element in Dart has to extend PolymerBase. To be able to access the members of PolymerBase from within the mixin it has to implement it as well. This doesn't result in any limitations because the classes that the mixin will be applied to, will fulfill that contract anyway.
If you don't need to access any members provided by PolymerBase there is no need to implement it.