Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are Keys in the Stateless widgets class?

Tags:

flutter

dart

In the flutter docs there's sample code for a stateless widget subclass as shown:

class GreenFrog extends StatelessWidget {   const GreenFrog({ Key key }) : super(key: key);    @override   Widget build(BuildContext context) {     return new Container(color: const Color(0xFF2DBD3A));   } } 

and this

class Frog extends StatelessWidget {   const Frog({     Key key,     this.color: const Color(0xFF2DBD3A),     this.child,   }) : super(key: key);    final Color color;    final Widget child;    @override   Widget build(BuildContext context) {     return new Container(color: color, child: child);   } } 

What is a key and when should this super constructor be used? It seems like if you have your own constructor you must have {Key key} why? I've seen other examples where the super keyword is not used so this is where my confusion is.

like image 277
DanT29 Avatar asked Apr 28 '18 20:04

DanT29


People also ask

What are keys in flutter widgets?

There are two types of keys in Flutter: GlobalKey s and LocalKey s. The different types of LocalKey s are: ValueKey: A key that uses a simple value such as a String. ObjectKey: A key that uses a more complex data format rather than a primitive data type like a String.

What is key in widget constructors?

A Key is an identifier for Widgets, Elements and SemanticsNodes. Cool, widgets have an identifier, and that's the Key .

What is key key in Dart?

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.

What is key and super key in flutter?

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).

What are keys for statelesswidgets?

Keys are IDs for widgets. All widgets have them, not just StatelessWidgets. They are used by the Element tree to determine if a widget can be reused or if it needs to be rebuilt. When no key is specified (the usual case), then the widget type is used to determine this.

What is a stateless widget?

A stateless widget is a widget that describes part of the user interface by building a constellation of other widgets that describe the user interface more concretely.

What is the use of key in widget?

Key is object that is used to identify a widget uniquely. They are used to access or restore state In a StatefulWidget (Mostly we don't need them at all if our widget tree is all Stateless Widgets). There are various types of key that I will try to explain on the basis of usage. Purpose (key types)

What is the state of the widget?

In other words, the state of the widget is the data of the objects that its properties (parameters) are sustaining at the time of its creation (when the widget is painted on the screen). The state can also change when it is used for example when a CheckBox widget is clicked a check appears on the box.


1 Answers

TLDR: All widgets should have a Key key as optional parameter or their constructor. Key is something used by flutter engine at the step of recognizing which widget in a list as changed.


It is useful when you have a list (Column, Row, whatever) of widgets of the same type that can potentially get removed/inserted.

Let's say you have this (code not working, but you get the idea) :

AnimatedList(   children: [     Card(child: Text("foo")),     Card(child: Text("bar")),     Card(child: Text("42")),   ] ) 

Potentially, you can remove any of these widgets individually with a swipe.

The thing is, our list has an animation when a child is removed. So let's remove "bar".

AnimatedList(   children: [     Card(child: Text("foo")),     Card(child: Text("42")),   ] ) 

The problem: Without Key, flutter won't be able to know if the second element of your Row disappeared. Or if it's the last one that disappeared and the second has its child change.

So without Key, you could potentially have a bug where your leave animation will be played on the last element instead!


This is where Key takes place.

If we start our example again, using key we'd have this :

AnimatedList(   children: [     Card(key: ObjectKey("foo"), child: Text("foo")),     Card(key: ObjectKey("bar"), child: Text("bar")),     Card(key: ObjectKey("42"), child: Text("42")),   ] ) 

notice how the key is not the child index but something unique to the element.

From this point, if we remove "bar" again, we'll have

AnimatedList(   children: [     Card(key: ObjectKey("foo"), child: Text("foo")),     Card(key: ObjectKey("42"), child: Text("42")),   ] ) 

Thanks to key being present, flutter engine now knows for sure which widget got removed. And now our leave animation will correctly play on "bar" instead of "42".

like image 99
Rémi Rousselet Avatar answered Sep 28 '22 02:09

Rémi Rousselet