Understanding keys

Flutter tips Published on

GlobalKey is a special type of key in Flutter. It's unique across your entire app. This means you should not create a new GlobalKey every time your widget builds. Instead, think of it as a long-lasting object that belongs to a widget's state.

Using GlobalKeys

Use Case 1: Accessing Widget State

One common use is to get access to the state of a stateful widget from outside that widget. A great example is validating a form. You can attach a GlobalKey to a Form widget to later get its FormState and call methods like validate().

final GlobalKey<FormState> _formKey = GlobalKey<FormState>(debugLabel: 'form');

// Later, to validate:
bool isFormValid() {
  if (_formKey.currentState == null) {
    return false;
  }
  return _formKey.currentState!.validate();
}

Use Case 2: Moving Widgets Without Losing State

GlobalKeys are also super helpful when you need a widget to move to a different spot in the widget tree without losing its current state.

This is often done using KeyedSubtree. By giving a widget a GlobalKey and wrapping it or its parent in KeyedSubtree, Flutter can recognize the widget even if its position changes. A classic example where this happens automatically is with Hero widgets, which smoothly animate between screens while keeping their state.

class _HeroState extends State<Hero> {
  final GlobalKey _key = GlobalKey();

  
  Widget build(BuildContext context) {
    return SizedBox(
      width: _placeholderSize?.width,
      height: _placeholderSize?.height,
      child: Offstage(
        offstage: showPlaceholder,
        child: TickerMode(
          enabled: !showPlaceholder,
          child: KeyedSubtree(key: _key, child: widget.child),
        ),
      ),
    );
  }
}

Save 3 months of work

Create your app using our 6 years of making Flutter apps and more than 50+ apps

kickstarter for flutter apps

Frequently Asked Questions

What is a GlobalKey?

A GlobalKey is a unique identifier for a widget in Flutter, unique across the whole application.

When should I use a GlobalKey?

Use it when you need to access a widget's state from outside, or when a widget needs to keep its state while changing location in the widget tree. Use it with care, as it can lead to performance issues if overused.

Should I create a GlobalKey on every build?

No, GlobalKeys are long-lived objects and should not be recreated on every build.

Can I access a widget's state using GlobalKey?

Yes, you can attach a GlobalKey to a widget and then use it to access its state, like getting the `FormState` from a `Form` widget.

Can a GlobalKey help a widget keep its state when its parent changes?

Yes, this is one of its key uses, often combined with `KeyedSubtree` or handled automatically by widgets like `Hero`.

Read more
You may also be interested in
6 ways to copy a list or map in dart  blog card image
6 ways to copy a list or map in dart
Published on 2025-05-12T11:41:57.081Z
How to show a notification Toast with Riverpod  blog card image
How to show a notification Toast with Riverpod
Published on 2025-05-01
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved