简体   繁体   English

错误:错误:没有为类型“_StoryPageState”定义方法“nextStory”

[英]Error: error: The method 'nextStory' isn't defined for the type '_StoryPageState'

I am creating an app using flutter, and I encountered the errors below.我正在使用 flutter 创建一个应用程序,我遇到了以下错误。 How can I fix this this?我该如何解决这个问题? I tried [hot restart][1] and flutter clean , but it didn't work.我尝试了[hot restart][1]flutter clean ,但没有成功。 I'm trying to create an app that shows different stories by the choice of user.我正在尝试创建一个应用程序,根据用户的选择显示不同的故事。 'nextStory' is defined in the story_brain.dart file. 'nextStory' 在 story_brain.dart 文件中定义。

Error错误

error: The method 'nextStory' isn't defined for the type '_StoryPageState'. (undefined_method at [destini_challenge_starting] lib\main.dart:57)
error: The method 'nextStory' isn't defined for the type '_StoryPageState'. (undefined_method at [destini_challenge_starting] lib\main.dart:78)

main.dart main.dart

import 'package:flutter/material.dart';
import 'story_brain.dart';

void main() => runApp(Destini());

class Destini extends StatelessWidget {
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark(),
      home: StoryPage(),
    );
  }
}


StoryBrain storyBrain = StoryBrain();

class StoryPage extends StatefulWidget {
  _StoryPageState createState() => _StoryPageState();
}

class _StoryPageState extends State<StoryPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage("images/background.png"),
            fit: BoxFit.cover,
          ),
        ),
        padding: EdgeInsets.symmetric(vertical: 50.0, horizontal: 15.0),
        constraints: BoxConstraints.expand(),
        child: SafeArea(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget>[
              Expanded(
                flex: 12,
                child: Center(
                  child: Text(
                    storyBrain.getStory(),
                    style: TextStyle(
                      fontSize: 25.0,
                    ),
                  ),
                ),
              ),
              Expanded(
                flex: 2,
                child: FlatButton(
                  onPressed: () {
                    //Choice 1 made by user.
                    nextStory(1);
                  },
                  color: Colors.red,
                  child: Text(
                    storyBrain.getChoice1(),
                    style: TextStyle(
                      fontSize: 20.0,
                    ),
                  ),
                ),
              ),
              SizedBox(
                height: 20.0,
              ),
              Expanded(
                flex: 2,
                child: FlatButton(
                  onPressed: () {
                    //Choice 2 made by user.
                    nextStory(2);
                  },
                  color: Colors.blue,
                  child: Text(
                    storyBrain.getChoice2(),
                    style: TextStyle(
                      fontSize: 20.0,
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}



story_brain.dart story_brain.dart

import 'story.dart';

class StoryBrain {

List<Story> _storyData = [
  Story(
      storyTitle:
      'Your car has blown a tire on a winding road in the middle of nowhere with no cell phone reception. You decide to hitchhike. A rusty pickup truck rumbles to a stop next to you. A man with a wide brimmed hat with soulless eyes opens the passenger door for you and asks: "Need a ride, boy?".',
      choice1: 'I\'ll hop in. Thanks for the help!',
      choice2: 'Better ask him if he\'s a murderer first.'),
  Story(
      storyTitle: 'He nods slowly, unphased by the question.',
      choice1: 'At least he\'s honest. I\'ll climb in.',
      choice2: 'Wait, I know how to change a tire.'),
 ...
];

String getStory(){
  return _storyData[0].storyTitle;
}

String getChoice1(){
  return _storyData[0].choice1;
}

String getChoice2(){
  return _storyData[0].choice2;
}


int storyNumber = 0;

void nextStory(int choiceNumber) {
  if (storyNumber == 0 && choiceNumber == 1) {
    storyNumber == 2;
  } else if (storyNumber == 0 && choiceNumber == 2) {
    storyNumber == 1;
  } else if (storyNumber == 1 && choiceNumber == 1) {
    storyNumber == 2;
  }else if(storyNumber==3||storyNumber==4||storyNumber==5){
    restart();
  }
}

void restart(){
  int storyNumber = 0;
}

}

story.dart故事.dart


class Story {
  String storyTitle;
  String choice1;
  String choice2;

  Story({this.storyTitle, this.choice1, this.choice2});

}



Thanks Anna for your comment!谢谢安娜的评论! I fixed the error!我修复了错误!

I used storyBrain.nextStory() instead of nextStory ().我使用了storyBrain.nextStory() 而不是nextStory()。

Comment from Anna安娜的评论

because funtion nextStory is define in StoryBrain not in StoryPage 

i think you want to do storyBrain.nextStory() instead of nextStory ()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:没有为该类型定义方法“onChanged” - error: The method 'onChanged' isn't defined for the type 错误:没有为“完成者”类型定义方法“setMapStyle” - error: The method 'setMapStyle' isn't defined for the type 'Completer' flutter 错误:没有为“Observable”类型定义方法“combineLatest4” - flutter error: The method 'combineLatest4' isn't defined for the type 'Observable' BloC 测试“未为类型‘对象’定义方法 xxx”错误 - BloC testing "The method xxx isn't defined for the type 'Object'" error 错误:未在 flutter 中为类型“FirebaseMessaging”定义方法“configure” - error: The method 'configure' isn't defined for the type 'FirebaseMessaging' in flutter Flutter 错误 - 未为该类型定义方法“setState” - Flutter Error - Method 'setState' isn't defined for the type Flutter 错误:没有为“ScreenUtil”类型定义方法“setContext” - Flutter Error: The method 'setContext' isn't defined for the type 'ScreenUtil' 错误 Flutter - 未为类型“FirebaseMessaging”定义方法“configure” - Error Flutter - The method 'configure' isn't defined for the type 'FirebaseMessaging' Dart Flutter 未为类型“sqliteDaatabase”定义方法“getDatabasePath”。 错误 - Dart Flutter The method 'getDatabasePath' isn't defined for the type 'sqliteDaatabase'. Error 错误:没有为 class 定义方法“flatbutton” - error: the method ´flatbutton´ isn´t defined for the class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM