title: About errors that occur in flutter's provider
body: <p>I'm learning about flutter's provider and I'm suffering from one error.</p>
  
  <p>The following code works.</p>
  
  <pre><code>[code1]
  class Model extends ChangeNotifier {
    void save() {
      print('save');
    }
  }
  class Main extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return ChangeNotifierProvider(
        create: (context) =&gt; Model(),
        child: Scaffold(
          appBar: AppBar(
            title: Text('test'),
          ),
          body: NewWidget(),
        ),
      );
    }
  }
  
  class NewWidget extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return RaisedButton(
        child: Text(
          "test",
        ),
        onPressed: () {
          context.read&lt;Model&gt;().save();
        },
      );
    }
  }
  </code></pre>
  
  <p>But the code below does not work.</p>
  
  <pre><code>[code2]
  class Model extends ChangeNotifier {
    void save() {
      print('save');
    }
  }
  
  class Main extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return ChangeNotifierProvider(
        create: (context) =&gt; Model(),
        child: Scaffold(
          appBar: AppBar(
            title: Text('test'),
          ),
          body: RaisedButton(
            child: Text(
              "test",
            ),
            onPressed: () {
              context.read&lt;Model&gt;().save();
            },
          ),
        ),
      );
    }
  }
  </code></pre>
  
  <p>With this code, the following error is output when the button is pressed.</p>
  
  <pre><code>Error: Could not find the correct Provider&lt;Model&gt; above this Main Widget
  
  This likely happens because you used a `BuildContext` that does not include the provider
  of your choice. There are a few common scenarios:
  
  - The provider you are trying to read is in a different route.
  
    Providers are "scoped". So if you insert of provider inside a route, then
    other routes will not be able to access that provider.
  
  - You used a `BuildContext` that is an ancestor of the provider you are trying to read.
  
    Make sure that Main is under your MultiProvider/Provider&lt;Model&gt;.
    This usually happen when you are creating a provider and trying to read it immediatly.
  
    For example, instead of:
  </code></pre>
  
  <p>I don't separate widgets, I want to write one widget like code2.<br>
  Please let me know if there is any good way.</p>
  
  <p>Thanks!</p>