Cyber Avater
Cyber Avater

Reputation: 2147

The instance member cannot be accessed in an initializer, try replacing the reference to the instance member with a different expression -alternative?

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.

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() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {


  nextStory(int choice) {
    setState(
          () {
        storyBrain.setStoryNo(choice);
      },
    );
  }
  List<Expanded> 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("images/background.png"),
            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,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Upvotes: 0

Views: 3459

Answers (1)

Nisanth Reddy
Nisanth Reddy

Reputation: 6395

Since your nextStory function is an instance member i.e., only accessible from an instance of your _MyHomePageState class, you cannot directly use it for initializing other instance variable which in your case is the buttonsWidgets.

One way to fix this is to move your buttonsWidgets inside the build method.

@override
Widget build(BuildContext context) {
  List<Expanded> buttonsWidgets = [/*same code*/];

Another way to fix this would be to make your buttonsWidgets into a function and then call it instead of using it directly.

List<Expanded> buttonsWidgets () {
  return [
    Expanded(
      child: Padding(
    ....
  ];
}

and then use it like this,

crossAxisAlignment: CrossAxisAlignment.stretch,
children: buttonsWidgets(),

Upvotes: 4

Related Questions