简体   繁体   English

该方法未为 class 定义 - Flutter

[英]The method isn't defined for the class - Flutter

I'm new to flutter and have been trying to make a simple quiz app.我是 flutter 的新手,一直在尝试制作一个简单的测验应用程序。 I've encountered an error and I'm not sure what is wrong.我遇到了一个错误,我不确定出了什么问题。

Error:错误:

Compiler message:                                                       
lib/main.dart:37:17: Error: The method 'Answer' isn't defined for the class '_MyAppState'.
 - '_MyAppState' is from 'package:project2/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing method, or defining a method named 'Answer'.
                Answer(),                                               
                ^^^^^^                                                  
lib/main.dart:38:17: Error: The method 'Answer' isn't defined for the class '_MyAppState'.
 - '_MyAppState' is from 'package:project2/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing method, or defining a method named 'Answer'.
                Answer(),                                               
                ^^^^^^                                                  
lib/main.dart:39:17: Error: The method 'Answer' isn't defined for the class '_MyAppState'.
 - '_MyAppState' is from 'package:project2/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing method, or defining a method named 'Answer'.
                Answer()                                                
                ^^^^^^     

main.dart:主要.dart:

import 'package:flutter/material.dart';
import './questions.dart';
import './answer.dart';

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

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MyAppState();
  }
}

class _MyAppState extends State<MyApp> {
  var _questionIndex = 0;

  var _questions = ["Question 1?", "Question 2?", "Question 3?"];

  void _answerQuestion() {
    setState(() {
      _questionIndex = _questionIndex + 1;
    });

    print("You answered the question!");
  }

  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            appBar: AppBar(
              title: Text("Quiz"),
            ),
            body: Column(
              children: <Widget>[
                Question(_questions[_questionIndex]),
                Answer(),
                Answer(),
                Answer()
              ],
            )));
  }
}

answer.dart:回答.dart:

import 'package:flutter/material.dart';

class Answer extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Container(
        width: double.infinity,
        color: Colors.blue,
        child: RaisedButton(child: Text("Answer 1"), onPressed: null));
  }
}

I have used the same class name and imported the right file into main.dart.我使用了相同的 class 名称并将正确的文件导入 main.dart。 I'm not sure what's wrong.我不确定出了什么问题。 Can someone please point it out to me.有人可以给我指出来吗? Thanks in advance!提前致谢!

I don't recommend writing imports by your self, when you have some widget or function that is not imported an error will be highlighted with indicating suggestions to fix that .我不建议您自己编写导入,当您有一些未导入的小部件或函数时,将突出显示错误并指示修复该问题的建议。 here is an example :这是一个例子:

  • call your function or widget, here I have a widget to call :调用你的函数或小部件,这里我有一个小部件可以调用:
    调用名为:ScreenLogin() 的小部件

  • as you can see an error is highlighted suggesting to import如您所见,突出显示了一个错误,建议导入

  • click that then the widget is import correctly单击然后小部件正确导入
    导入的小部件

this will prevent such errors for you in the future这将防止您将来出现此类错误

I'm unable to replicate the same error that you received without a complete minimal repro and steps to reproduce the behavior.如果没有完整的最小重现和重现行为的步骤,我无法复制您收到的相同错误。 In my testing, an error occurred when import 'answer.dart';在我的测试中, import 'answer.dart';时出现错误import 'answer.dart'; was manually added in main.dart and ran the app with hot reload.在 main.dart 中手动添加并通过热重载运行应用程序。 Since it was a hot reload and the import was manually added, it's likely that code changes weren't included, causing the issue.由于它是热重载并且手动添加了导入,因此很可能未包含代码更改,从而导致了问题。 The app works fine after a restart, and the issue doesn't occur when you let the IDE auto-complete the import.该应用程序在重新启动后工作正常,并且当您让 IDE 自动完成导入时不会出现该问题。

With the current details, I'm unable to rule out any definite cause for your issue.根据当前的详细信息,我无法排除您的问题的任何明确原因。

I'm running on Flutter stable channel version 1.22.0我在 Flutter 稳定频道版本 1.22.0 上运行

Error I got:我得到的错误:

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following _CompileTimeError was thrown building MyApp(dirty, state: _MyAppState#2340f):
Unimplemented handling of missing static target

The relevant error-causing widget was: 
  MyApp file:///Users/{USER}/Downloads/dev/flutter/sample60384439/lib/main.dart:5:23
When the exception was thrown, this was the stack: 
#0      StatefulElement.build (package:flutter/src/widgets/framework.dart:4744:28)
#1      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4627:15)
#2      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4800:11)
#3      Element.rebuild (package:flutter/src/widgets/framework.dart:4343:5)
#4      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2730:33)
...
════════════════════════════════════════════════════════════════════════════════════════════════════
Reloaded 2 of 530 libraries in 220ms.

Not same but similar error.不一样但类似的错误。 I did a hot restart instead of a hot reload.我进行了热重启而不是热重载。 note: Windows 10, Android Studio IDE注意:Windows 10,Android Studio IDE

I to had a similar issue.我有一个类似的问题。 Even though my import statement is correct i had this issue.即使我的导入声明是正确的,我也遇到了这个问题。

Then i have did "flutter clean" and after "flutter run" it worked.然后我做了“flutter clean”,在“flutter run”之后它起作用了。

So, just clean the build using "flutter clean" and see.因此,只需使用“flutter clean”清理构建并查看。 hope it works.希望它有效。

I tried all the solutions given above and nothing worked for me.This is the problem with the Flutter SDK and the Dart versions.我尝试了上面给出的所有解决方案,但没有任何效果。这是 Flutter SDK 和 Dart 版本的问题。 Finally upgraded the flutter sdk and that did the trick.终于升级了 flutter sdk 并且成功了。 This was suggested here GitHub solution这是此处建议的GitHub 解决方案

flutter upgrade --force

Same Problem...同样的问题...

I fixed it by removing.pub-cache/ folder and ran flutter clean我通过删除 .pub-cache/ 文件夹修复它并运行 flutter clean

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM