> For the complete documentation index, see [llms.txt](https://ecm-pmdm-flutter.gitbook.io/1.-introduccion-a-flutter/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ecm-pmdm-flutter.gitbook.io/1.-introduccion-a-flutter/4.-conceptos-fundamentales-en-flutter/ciclo-de-vida/responding-to-widget-lifecycle-events-keys.md).

# Responding to widget lifecycle events, Keys

{% hint style="info" %}
<https://docs.flutter.dev/development/ui/widgets-intro>

<https://www.raywenderlich.com/22416843-unlocking-your-flutter-widgets-with-keys>
{% endhint %}

After calling [`createState()`](https://api.flutter.dev/flutter/widgets/StatefulWidget-class.html#createState) on the `StatefulWidget`, the framework inserts the new state object into the tree and then calls [`initState()`](https://api.flutter.dev/flutter/widgets/State-class.html#initState) on the state object. A subclass of [`State`](https://api.flutter.dev/flutter/widgets/State-class.html) can override `initState` to do work that needs to happen just once. For example, override `initState` to configure animations or to subscribe to platform services. Implementations of `initState` are required to start by calling `super.initState`.

When a state object is no longer needed, the framework calls [`dispose()`](https://api.flutter.dev/flutter/widgets/State-class.html#dispose) on the state object. Override the `dispose` function to do cleanup work. For example, override `dispose` to cancel timers or to unsubscribe from platform services. Implementations of`dispose` typically end by calling `super.dispose`.

For more information, see [`State`](https://api.flutter.dev/flutter/widgets/State-class.html).

### Keys <a href="#keys" id="keys"></a>

Use keys to control which widgets the framework matches up with other widgets when a widget rebuilds. By default, the framework matches widgets in the current and previous build according to their [`runtimeType`](https://api.flutter.dev/flutter/widgets/Widget-class.html#runtimeType) and the order in which they appear. **With keys, the framework requires that the two widgets have the same** [**`key`**](https://api.flutter.dev/flutter/foundation/Key-class.html) **as well as the same `runtimeType`.**

**Keys are most useful in widgets that build many instances of the same type of widget**. For example, the i a `ShoppingList` widget, which builds just enough `ShoppingListItem` instances to fill its visible region:

* Without keys, the first entry in the current build would always sync with the first entry in the previous build, even if, semantically, the first entry in the list just scrolled off screen and is no longer visible in the viewport.
* By assigning each entry in the list a “semantic” key, the infinite list can be more efficient because the framework syncs entries with matching semantic keys and therefore similar (or identical) visual appearances. Moreover, **syncing the entries semantically means that state retained in stateful child widgets remains attached to the same semantic entry rather than the entry in the same numerical position in the viewport**.

For more information, see the [`Key`](https://api.flutter.dev/flutter/foundation/Key-class.html) API.

### Types of keys <a href="#global-keys" id="global-keys"></a>

There are two types of keys in Flutter: `GlobalKey`s and `LocalKey`s.  &#x20;

* Local keys are unique only among siblings. &#x20;
* Global keys are globally unique across the entire widget hierarchy.

#### 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.
* *UniqueKey*: A key that is unique only to itself. Use this type of key when you don't have any other data that makes a widget unique when using a `ValueKey` or an `ObjectKey`.

#### Global keys <a href="#global-keys" id="global-keys"></a>

Use global keys to uniquely identify child widgets. , unlike local keys which need only be unique among siblings.

Because they are globally unique, a global key can be used to retrieve the state associated with a widget.

For more information, see the [`GlobalKey`](https://api.flutter.dev/flutter/widgets/GlobalKey-class.html) API.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ecm-pmdm-flutter.gitbook.io/1.-introduccion-a-flutter/4.-conceptos-fundamentales-en-flutter/ciclo-de-vida/responding-to-widget-lifecycle-events-keys.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
