title: dependOnInheritedElement() was called before initstate() in flutter
body: <p>I am currently having an issue while fetching a <code>Provider' value in</code>initstate`. </p>
  
  <p>I want to set a default value in dropdown in an Appbar and other parts in body. But I got an error saying <code>dependOnInheritedElement() was called before initstate() in flutter</code>.</p>
  
  <p>My full code is below</p>
  
  <p><code>main.dart</code></p>
  
  <pre><code>import 'package:test_eoil/model/button_data.dart';
  import 'package:test_eoil/model/output_data.dart';
  import 'screen/screen.dart';
  import 'package:flutter/material.dart';
  import 'package:provider/provider.dart';
  
  
  void main() =&gt; runApp(new MyApp());
  
  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return MultiProvider(providers: [
  //      ChangeNotifierProvider&lt;ChordData&gt;(create: (context) =&gt; ChordData()),
        ChangeNotifierProvider&lt;OutputData&gt;(create: (context) =&gt; OutputData()),
        ChangeNotifierProvider&lt;ButtonData&gt;(create: (context) =&gt; ButtonData())
      ],
        child: MaterialApp(
          home: Screen(),
        ),
      );
    }
  }
  </code></pre>
  
  <p><code>screen.dart</code> in screen folder</p>
  
  <pre><code>import 'package:flutter/material.dart';
  import 'package:provider/provider.dart';
  import 'package:test_eoil/model/button_data.dart';
  import 'package:test_eoil/model/output_data.dart';
  
  
  class Screen extends StatefulWidget {
    @override
    _ScreenState createState() =&gt; _ScreenState();
  }
  
  class _ScreenState extends State&lt;Screen&gt; {
  
    Widget dropdownWidget() {
      return DropdownButton&lt;Button&gt;(
        items: Provider.of&lt;ButtonData&gt;(context).buttons.map((Button value) {
          return new DropdownMenuItem&lt;Button&gt;(
            value: value,
            child: new Text(value.type.toString()),
          );
        }).toList(),
        onChanged: (Button newValue) {
          Provider.of&lt;ButtonData&gt;(context).setSelectedItem(newValue);
        },
        value: Provider.of&lt;ButtonData&gt;(context).selectedButton,
      );
    }
  
    @override
    void initState() {
      Provider.of&lt;ButtonData&gt;(context).selectedButton = Provider.of&lt;ButtonData&gt;(context).buttons.first;
      super.initState();
    }
  
  
    @override
    Widget build(BuildContext context) {
      return Consumer&lt;OutputData&gt;(
          builder: (context, outputData, child) =&gt; Scaffold(
              appBar: AppBar(
                title: Text("${Provider.of&lt;ButtonData&gt;(context).selectedButton}"), // new Text(widget.title), // "${Provider.of&lt;ButtonData&gt;(context).selectedButton.key}"
                actions: &lt;Widget&gt;[
                  dropdownWidget(),
                ],
              ),
              body: Column(
                children: &lt;Widget&gt;[
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    mainAxisSize: MainAxisSize.max,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: &lt;Widget&gt;[
                      Container(
                        alignment: Alignment.centerRight,
                        padding: new EdgeInsets.symmetric(
                            vertical: 24.0, horizontal: 12.0),
                        child: outputData.outputs[0].output3.length &gt; 2
                            ? Text(
                            '${outputData.outputs[0].output3.substring(1, outputData.outputs[0].output3.length-1)}',
                            style: TextStyle(
                              fontSize: 35.0,
                              fontWeight: FontWeight.bold,
                            ))
                            : Text('${outputData.outputs[0].output3}',
                            style: TextStyle(
                              fontSize: 35.0,
                              fontWeight: FontWeight.bold,
                            )),
                      ),
                      Container(
                        alignment: Alignment.centerRight,
                        padding: new EdgeInsets.symmetric(
                            vertical: 24.0, horizontal: 12.0),
                        child: outputData.outputs[0].output2.length &gt; 2
                            ? Text(
                            '${outputData.outputs[0].output2.substring(1, outputData.outputs[0].output2.length-1)}',
                            style: TextStyle(
                              fontSize: 35.0,
                              fontWeight: FontWeight.bold,
                            ))
                            : Text('${outputData.outputs[0].output2}',
                            style: TextStyle(
                              fontSize: 35.0,
                              fontWeight: FontWeight.bold,
                            )),
                      ),
                      Container(
                        alignment: Alignment.centerRight,
                        padding: new EdgeInsets.symmetric(
                            vertical: 24.0, horizontal: 12.0),
                        child: Text('${outputData.outputs[0].output1}',  //  outputData.outputs[0].output1
                            style: TextStyle(
                              fontSize: 35.0,
                              fontWeight: FontWeight.bold,
                            )),
                      ),
                    ],
                  ),
                  Expanded(
                    child: new Divider(),
                  ),
                  Column(children: [
                    Row(children: [
                      buildButton("CLEAR"),
                      buildButton(""),
                      buildButton("PLAY"),
                      //                    buildButton("/")
                    ]),
                  ])
                ],
              )));
    }
  
  
  
    Widget buildButton(String buttonText) {
      return new Expanded(
        child: new OutlineButton(
          padding: new EdgeInsets.all(30.0),
          child: new Text(
            buttonText,
            style: TextStyle(fontSize: 20.0),
          ),
          onPressed: () =&gt; print("test"),
        ),
      );
    }
  }
  
  </code></pre>
  
  <p><code>button_data.dart</code> in model folder</p>
  
  <pre><code>import 'package:flutter/foundation.dart';
  //import 'dart:collection';
  
  class Button {
    final int id;
    final String type;
    final String numberone;
    final String numbertwo;
    final String numberthree;
  
    Button({this.id, this.type, this.numberone, this.numbertwo, this.numberthree});
  
  
  }
  
  class ButtonData extends ChangeNotifier {
    List&lt;Button&gt; _buttons = [
      Button(
        type: "A",
        numberone: "1",
        numbertwo: "2",
        numberthree: "3",
      ),
      Button(
        type: "B",
        numberone: "A",
        numbertwo: "B",
        numberthree: "C",
      ),
    ];
  
    List&lt;Button&gt; get buttons =&gt; _buttons;
    Button _selectedButton;
    Button get selectedButton =&gt; _selectedButton;
  
    set selectedButton(Button button) {
      _selectedButton = button;
      notifyListeners();
    }
  
  
    void setSelectedItem(Button s) {
      _selectedButton = s;
      notifyListeners();
    }
  
    Button getKey(String value) {
      return _buttons
          .where((button) =&gt; button.type == value).first;
    }
  
    String getNumberOne(String value) {
      return _buttons
          .where((button) =&gt; button.type == value)
          .map((button) =&gt; (button.numberone))
          .toString();
    }
  
    String getNumberTwo(String value) {
      return _buttons
          .where((button) =&gt; button.type == value)
          .map((button) =&gt; (button.numbertwo))
          .toString();
    }
  
    String getNumberThree(String value) {
      return _buttons
          .where((button) =&gt; button.type == value)
          .map((button) =&gt; (button.numberthree))
          .toString();
    }
  
  }
  </code></pre>
  
  <p><code>output_data.dart</code> in model folder</p>
  
  <pre><code>import 'package:flutter/foundation.dart';
  
  
  class Output {
  
    final int id;
    String output1;
    String output2;
    String output3;
  
    Output({this.id, this.output1, this.output2, this.output3});
  
  }
  
  
  class OutputData extends ChangeNotifier {
  
    List&lt;Output&gt; _outputs = [
      Output(output1: 'Hello', output2: 'Hi', output3: 'Nice'),
      Output(output1: 'Haha', output2: 'Bye', output3: 'Sad'),
    ];
  
    List&lt;Output&gt; get outputs {
      return _outputs;
    }
  
  
  }
  
  </code></pre>
  
  <p>To be honest, I want to make it work without <code>initstate()</code> if possible(I heard that provider pattern doesn't need stful)</p>
  
  <p>The reason I come up with an <code>initstate()</code> is this is the only solution (as much as I know) to set the default value in provider.</p>
  
  <p>Hope you guys help me!</p>
  
  <hr>
  
  <p>Issue is solved by adding Button_data constructor in <code>button_data.dart</code>.</p>
  
  <pre><code>ButtonData () {
      _selectedButton = _buttons.first;
    }
  </code></pre>