title: The instance member cannot be accessed in an initializer, try replacing the reference to the instance member with a different expression -alternative?
body: <p>I want onPressed() function to run nextStory() (so that I don't have to repeat the code more than once), but it says instance member 'nextStory' cannot be accessed in an initializer. Can anyone help to achieve this? I'm using the latest flutter and dart. I'm sharing my code (which is giving this error) to better explain the issue.
  P.S. I've also marked where to focus via comment.</p>
  <pre><code>import 'package:flutter/cupertino.dart';
  import 'package:flutter/material.dart';
  import 'story_brain.dart';
  
  StoryBrain storyBrain = StoryBrain();
  
  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 Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: MyHomePage(),
      );
    }
  }
  
  class MyHomePage extends StatefulWidget {
    const MyHomePage({Key key}) : super(key: key);
  
    @override
    _MyHomePageState createState() =&gt; _MyHomePageState();
  }
  
  class _MyHomePageState extends State&lt;MyHomePage&gt; {
  
  
    nextStory(int choice) {
      setState(
            () {
          storyBrain.setStoryNo(choice);
        },
      );
    }
    List&lt;Expanded&gt; buttonsWidgets = [
      Expanded(
        child: Padding(
          padding: EdgeInsets.all(10),
          child: ElevatedButton(
            onPressed: () {
              nextStory(1); //Focus here
            },
            style: ElevatedButton.styleFrom(
              primary: Colors.red,
            ),
            child: Text(
              storyBrain.getChoice1(),
              style: TextStyle(
                color: Colors.white,
                fontSize: 24,
              ),
            ),
          ),
        ),
      ),
      Expanded(
        child: Padding(
          padding: EdgeInsets.all(10),
          child: ElevatedButton(
            onPressed: () {
              nextStory(2); //Focus here
            },
            style: ElevatedButton.styleFrom(
              primary: Colors.blue,
            ),
            child: Text(
              storyBrain.getChoice2(),
              style: TextStyle(
                color: Colors.white,
                fontSize: 24,
              ),
            ),
          ),
        ),
      ),
    ];
  
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: Container(
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage(&quot;images/background.png&quot;),
              fit: BoxFit.cover,
            ),
          ),
          child: Column(
            children: [
              Flexible(
                flex: 5,
                child: Container(
                  child: Center(
                    child: Text(
                      storyBrain.getStory(),
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 24,
                      ),
                    ),
                  ),
                ),
              ),
              Flexible(
                flex: 2,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: buttonsWidgets,
                ),
              ),
            ],
          ),
        ),
      );
    }
  }
  </code></pre>