mounted property

bool get mounted

Whether the Widget this context is associated with is currently mounted in the widget tree.

Accessing the properties of the BuildContext or calling any methods on it is only valid while mounted is true. If mounted is false, assertions will trigger.

Once unmounted, a given BuildContext will never become mounted again.

If a BuildContext is used across an asynchronous gap (i.e. after performing an asynchronous operation), consider checking mounted to determine whether the context is still valid before interacting with it:

  @override
  Widget build(BuildContext context) {
    return OutlinedButton(
      onPressed: () async {
        await Future<void>.delayed(const Duration(seconds: 1));
        if (context.mounted) {
          Navigator.of(context).pop();
        }
      },
      child: const Text('Delayed pop'),
    );
  }

Implementation

bool get mounted;