没有为类定义方法- Flutter

fdx2calv  于 2022-12-30  发布在  Flutter
关注(0)|答案(6)|浏览(226)

我是flutter的新手,一直在尝试做一个简单的问答应用程序。我遇到了一个错误,我不知道是什么错误。

    • 错误:**
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()                                                
                ^^^^^^
    • 主省道:**
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()
              ],
            )));
  }
}
    • 答案。 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));
  }
}

我使用了相同的类名,并且导入了正确的文件到main.dart中。我不确定是什么问题。请有人给我指出。提前感谢!

vdzxcuhz

vdzxcuhz1#

我不建议你自己写导入,当你有一些小部件或函数没有导入一个错误将突出显示,并指出建议,以解决这一问题。这里是一个例子:

  • 调用您的函数或小部件,这里我有一个要调用的小部件:

  • 如您所见,突出显示了一个错误,建议导入
  • 单击,然后正确导入小部件

这将防止您将来发生此类错误

whhtz7ly

whhtz7ly2#

我有一个类似的问题。即使我的导入声明是正确的,我也有这个问题。
然后我做了“扑干净”,经过“扑运行”它的工作。
所以,只要用“flutter clean”来清理构建就可以了,希望它能起作用。

klr1opcd

klr1opcd3#

我尝试了上面给出的所有解决方案,没有一个对我有效。这是Flutter SDK和Dart版本的问题。最后升级了Flutter SDK,这就成功了。这里建议使用GitHub solution

flutter upgrade --force
nhn9ugyo

nhn9ugyo4#

如果没有完整的最低限度重现和重现该行为的步骤,我无法重现您收到的相同错误。在我的测试中,在main.dart中手动添加import 'answer.dart';并使用热重新加载运行应用程序时发生错误。由于这是一次热重新加载,并且导入是手动添加的,因此很可能没有包含代码更改,从而导致了该问题。重启后,应用程序工作正常。并且在让IDE自动完成导入时不会出现此问题。
根据目前的详细情况,我无法排除任何明确的原因造成您的问题。
我正在Flutter稳定通道版本1.22.0上运行
我遇到的错误:

════════ 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.
n3h0vuf2

n3h0vuf25#

不相同但相似的错误。我执行了热重新启动而不是热重新加载。注意:Windows 10、Android Studio 集成开发环境

bihw5rsg

bihw5rsg6#

同样的问题...
我通过删除.pub-cache/ folder修复了它,并运行flutter clean

相关问题