title: Flutter : No ScaffoldMessenger widget found
body: <p>I am trying to create a snackbar on the click of a button in flutter, but getting exception that No <code>ScaffoldMessenger</code> widget found. The same code seems to work properly in the Flutter <a href="https://flutter.dev/docs/cookbook/forms/validation" rel="noreferrer">sample</a> mentioned in docs. Am I missing something here? Thanks.</p>
  <p>Here's my main.dart file</p>
  <pre><code>import 'package:flutter/material.dart';
  
  void main() =&gt; runApp(MyAppWidget());
  
  class MyAppWidget extends StatefulWidget {
    @override
    State&lt;StatefulWidget&gt; createState() {
      return _MyAppState();
    }
  }
  
  class _MyAppState extends State&lt;MyAppWidget&gt; {
    final _inputKey = GlobalKey&lt;FormState&gt;();
    final _messangerKey = GlobalKey&lt;ScaffoldMessengerState&gt;();
    String inputText = &quot;&quot;;
  
    String appendString() {
      setState(() {
        inputText += inputText;
      });
      return inputText;
    }
  
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        scaffoldMessengerKey: _messangerKey,
        home: Scaffold(
          appBar: AppBar(
            title: Text(&quot;Assignment&quot;),
          ),
          body: Form(
            key: _inputKey,
            child: Column(
              children: [
                TextFormField(
                  validator: (inputString) {
                    inputText = inputString;
                    if (inputString.length &lt; 5) {
                      return 'Please enter a longer string';
                    }
                    return null;
                  },
                ),
                ElevatedButton(
                  onPressed: () {
                    if (_inputKey.currentState.validate()) {
                      ScaffoldMessenger.of(context).showSnackBar(
                          SnackBar(content: Text('Processing Data')));
                    }
                  },
                  child: Text(&quot;Enter&quot;),
                ),
                Text(appendString())
              ],
            ),
          ),
        ),
      );
    }
  }
  </code></pre>
  <p>Here's the exception I am getting</p>
  <pre><code>════════ Exception caught by gesture ═══════════════════════════════════════════
  The following assertion was thrown while handling a gesture:
  No ScaffoldMessenger widget found.
  
  MyAppWidget widgets require a ScaffoldMessenger widget ancestor.
  
  Handler: &quot;onTap&quot;
  
  #4      _InkResponseState._handleTap
  package:flutter/…/material/ink_well.dart:991
  #3      _MyAppState.build.&lt;anonymous closure&gt;
  package:assignment_1/main.dart:48
  #2      ScaffoldMessenger.of
  package:flutter/…/material/scaffold.dart:224
  #1      debugCheckHasScaffoldMessenger
  package:flutter/…/material/debug.dart:153
  #0      debugCheckHasScaffoldMessenger.&lt;anonymous closure&gt;
  package:flutter/…/material/debug.dart:142
  When the exception was thrown, this was the stack
  
  Typically, the ScaffoldMessenger widget is introduced by the MaterialApp at the top of your application widget tree.
  
          renderObject: RenderView#a5074
      [root]
  The ancestors of this widget were
      state: _MyAppState#a2cb9
  Restarted application in 691ms.
  
  ════════ Exception caught by gesture ═══════════════════════════════════════════
  The following assertion was thrown while handling a gesture:
  No ScaffoldMessenger widget found.
  
  MyAppWidget widgets require a ScaffoldMessenger widget ancestor.
  The specific widget that could not find a ScaffoldMessenger ancestor was: MyAppWidget
  
  </code></pre>