Sub topic 6
BuildContext Is an Element
Assume you know basic Dart and can compose widgets such as Row, Column, Padding, and Text. You should also be familiar with StatelessWidget and StatefulWidget.
A BuildContext is a handle to a widget's location in the tree. Flutter passes it to build:
Widget build(BuildContext context)The important detail is that BuildContext is not the widget. In Flutter, an Element implements BuildContext. Each mounted widget has a corresponding element, and that element provides the context used by the widget.
What the context lets you do
A context gives Flutter the current position for operations such as:
- Finding inherited values with
Theme.of(context)orMediaQuery.of(context). - Looking up a
Navigator,Scaffold, or other ancestor. - Reading the current
Elementrelationship. - Checking whether the location is still mounted.
For example, Theme.of starts at the element represented by context and searches upward for a matching inherited widget:
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
theme: ThemeData(colorSchemeSeed: Colors.indigo),
home: const ContextDemo(),
),
);
}
class ContextDemo extends StatelessWidget {
const ContextDemo({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('BuildContext')),
body: Builder(
builder: (innerContext) {
final color = Theme.of(innerContext).colorScheme.primary;
return Center(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
foregroundColor: color,
),
onPressed: () {
showDialog<void>(
context: innerContext,
builder: (dialogContext) {
return AlertDialog(
title: const Text('Dialog'),
content: const Text('This context found the Navigator.'),
actions: [
TextButton(
onPressed: () => Navigator.pop(dialogContext),
child: const Text('Close'),
),
],
);
},
);
},
child: const Text('Open dialog'),
),
);
},
),
);
}
}Builder creates another element and supplies its context to the callback. This can matter when the context must be below a particular ancestor. The innerContext above is below the Scaffold, so it can find the route's Navigator and inherited theme.
Context lifetime and common errors
A context is valid while its element is mounted. Do not retain a context and use it much later without checking its lifetime. This matters after asynchronous work:
final result = await loadData();
if (!context.mounted) return;
Navigator.of(context).pop(result);In a State object, context refers to that state's StatefulElement. The State object can survive rebuilds, while its widget configuration changes through widget.
A context also does not guarantee that a desired ancestor exists. Navigator.of(context) fails if no navigator is above that location. Use a context from the correct subtree, or provide the required ancestor.
Exercise
Why might a Builder be useful when calling Navigator.of(context)?
Show answer
Builder supplies a context from a newly created element at that exact location, so the context can see ancestors that the surrounding widget's context cannot.