# Flutter: Scaffold.of() called with a context that does not contain a Scaffold

**Code:** `Scaffold.of() called with a context that does not contain a Scaffold` · **Category:** Flutter / Dart · **Seen on:** Flutter

You called `Scaffold.of(context)` (or `ScaffoldMessenger`) using the same `BuildContext` that created the Scaffold, which is above it in the tree.

## Meaning

`of(context)` looks upward from the context. The context of the widget that builds the `Scaffold` is its parent, so it cannot find it. Use a `Builder` widget to get a context below the Scaffold, or split the body into its own widget. For snack bars use `ScaffoldMessenger.of(context)` from a context under MaterialApp.

## Common causes

- of(context) in the same build method that returns the Scaffold
- Using the State's context from before Scaffold
- Calling from initState or a static function
- Context taken from a different route
- Old Scaffold.of(context).showSnackBar API

## Quick fix

1. Wrap the call site in `Builder(builder: (context) => …)`
2. Extract the body into a separate widget
3. Use `ScaffoldMessenger.of(context).showSnackBar(...)`

## Detailed fix by platform

### Flutter

1. `Scaffold(body: Builder(builder: (ctx) => TextButton(onPressed: () => ScaffoldMessenger.of(ctx).showSnackBar(const SnackBar(content: Text('Hi'))), child: const Text('Show'))))`

## How to diagnose

1. **Context** — Above or below Scaffold?
2. **API** — ScaffoldMessenger?
3. **Route** — Same route?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/flutter/scaffold-of-no-scaffold-context)
