of method

ScaffoldState of (BuildContext context, { bool nullOk: false })

The state from the closest instance of this class that encloses the given context.

Typical usage of the Scaffold.of function is to call it from within the build method of a child of a Scaffold.
import 'package:flutter/material.dart';

// ...

void main() => runApp(MyApp());

// ...

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Code Sample for Scaffold.of.',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        body: MyScaffoldBody(),
        appBar: AppBar(title: Text('Scaffold.of Example')),
      ),
      color: Colors.white,
    );
  }
}

// ...

class MyScaffoldBody extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: RaisedButton(
        child: Text('SHOW A SNACKBAR'),
        onPressed: () {
          Scaffold.of(context).showSnackBar(
            SnackBar(
              content: Text('Have a snack!'),
            ),
          );
        },
      ),
    );
  }
}

When the Scaffold is actually created in the same build function, the context argument to the build function can't be used to find the Scaffold (since it's "above" the widget being returned in the widget tree). In such cases, the following technique with a Builder can be used to provide a new scope with a BuildContext that is "under" the Scaffold:
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: Text('Demo')
    ),
    body: Builder(
      // Create an inner BuildContext so that the onPressed methods
      // can refer to the Scaffold with Scaffold.of().
      builder: (BuildContext context) {
        return Center(
          child: RaisedButton(
            child: Text('SHOW A SNACKBAR'),
            onPressed: () {
              Scaffold.of(context).showSnackBar(SnackBar(
                content: Text('Have a snack!'),
              ));
            },
          ),
        );
      },
    ),
  );
}

A more efficient solution is to split your build function into several widgets. This introduces a new context from which you can obtain the Scaffold. In this solution, you would have an outer widget that creates the Scaffold populated by instances of your new inner widgets, and then in these inner widgets you would use Scaffold.of.

A less elegant but more expedient solution is assign a GlobalKey to the Scaffold, then use the key.currentState property to obtain the ScaffoldState rather than using the Scaffold.of function.

If there is no Scaffold in scope, then this will throw an exception. To return null if there is no Scaffold, then pass nullOk: true.

Implementation

static ScaffoldState of(BuildContext context, { bool nullOk = false }) {
  assert(nullOk != null);
  assert(context != null);
  final ScaffoldState result = context.ancestorStateOfType(const TypeMatcher<ScaffoldState>());
  if (nullOk || result != null)
    return result;
  throw FlutterError(
    'Scaffold.of() called with a context that does not contain a Scaffold.\n'
    'No Scaffold ancestor could be found starting from the context that was passed to Scaffold.of(). '
    'This usually happens when the context provided is from the same StatefulWidget as that '
    'whose build function actually creates the Scaffold widget being sought.\n'
    'There are several ways to avoid this problem. The simplest is to use a Builder to get a '
    'context that is "under" the Scaffold. For an example of this, please see the '
    'documentation for Scaffold.of():\n'
    '  https://api.flutter.dev/flutter/material/Scaffold/of.html\n'
    'A more efficient solution is to split your build function into several widgets. This '
    'introduces a new context from which you can obtain the Scaffold. In this solution, '
    'you would have an outer widget that creates the Scaffold populated by instances of '
    'your new inner widgets, and then in these inner widgets you would use Scaffold.of().\n'
    'A less elegant but more expedient solution is assign a GlobalKey to the Scaffold, '
    'then use the key.currentState property to obtain the ScaffoldState rather than '
    'using the Scaffold.of() function.\n'
    'The context used was:\n'
    '  $context'
  );
}