I'm doing the Udacity flutter course. There's this constructor call.
child: Category(
name: _categoryName,
color: _categoryColor,
iconLocation: _categoryIcon,
),
When I did this on my own, I naturally wrote the constructor as follows:
const Category({
@required this.name,
@required this.icon,
@required this.color
}) : assert(name != null),
assert(icon != null),
assert(color != null);
Disregard the asserts and @requireds. You call using three parameters, so the constructor must have three parameters.
However, in the solution file of this exercise, the instructors did it like this.
const Category({
Key key,
@required this.name,
@required this.color,
@required this.iconLocation,
}) : assert(name != null),
assert(color != null),
assert(iconLocation != null),
super(key: key);
What is this key parameter and why is the parent of the category widget class (StatelessWidget I presume) is being passed it?
I have looked at Key class, but I did not understand anything. There's no context in this page or an example I can use.
Copy constructor has the same form as other constructors, except it has a reference paramter of the same class type as the object itself. C++ requires that a copy constructor's parameter be a reference object. Because copy constructor are required to use reference paramters, they have access to their argument's data.
The key identifies a widget, and this tells flutter whether a widget should be inflated new, or whether it should replace an existing widget in the tree during a build. Keys must be unique amongst the Elements with the same parent.
This method has four parameters: the loan amount, the interest rate, the future value and the number of periods.
A copy constructor has one parameter that is a reference to the type that is copied. It can have additional parameters, if these have default values.
Let's go through the key documentation you linked..
A Key is an identifier for Widgets, Elements and SemanticsNodes.
Cool, widgets have an identifier, and that's the Key
. Easy.
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.
Now this is a bit more cryptic, let's break it down.
What's happening here is when there's a change in state and your widget tree is being rebuilt, flutter needs a way to know:
The key identifies a widget, and this tells flutter whether a widget should be inflated new, or whether it should replace an existing widget in the tree during a build.
Keys must be unique amongst the Elements with the same parent.
Now this becomes obvious, since we can't know which state belongs to this widget if it's key is not unique. Try it yourself! Create a column with two widgets and give them the same key. Spoiler alert: flutter will complain, and you now know why :)
For completeness one last point to be made here is about local and global keys. A regular Key
is most probably a LocalKey
, this one just needs be unique among its siblings. If you want to retain state for a widget that moves around the tree changing parents, then you're looking for a GlobalKey
.
Also, flutter uses the type of the object (as in Object.runtimeType
) along with the key to identify widgets during a build. This is why you often don't specify a key at all and things still work, because these are likely LocalKeys
and you likely don't have two widgets of the same runtimeType
under the same parent, so even if they happen to have the same default key they still don't clash.
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