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

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

Quick Answer

The BuildContext used does not have a Scaffold ancestor in the widget tree.

Why This Happens

In Flutter, Scaffold.of(context) traverses up the widget tree to find a Scaffold. If the context belongs to the widget that builds the Scaffold itself, no Scaffold ancestor exists. Use ScaffoldMessenger.of(context) for showing snackbars, or use a Builder widget to get a context below the Scaffold.

The Problem

class MyPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ElevatedButton(
        onPressed: () {
          Scaffold.of(context).showBottomSheet(...); // Wrong context
        },
        child: Text('Show'),
      ),
    );
  }
}

The Fix

class MyPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Builder(
        builder: (scaffoldContext) {
          return ElevatedButton(
            onPressed: () {
              Scaffold.of(scaffoldContext).showBottomSheet(...);
            },
            child: Text('Show'),
          );
        },
      ),
    );
  }
}

Step-by-Step Fix

  1. 1

    Identify the error

    Look at the error message: Scaffold.of() called with a context that does not contain a Scaffold. The context is at or above the Scaffold.

  2. 2

    Find the cause

    Check if the context passed to Scaffold.of() belongs to the widget that creates the Scaffold, rather than a child of the Scaffold.

  3. 3

    Apply the fix

    Use a Builder widget to obtain a context below the Scaffold, or use ScaffoldMessenger.of(context) for snackbars.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free