未为该类定义该方法 - Flutter

Vai*_*v79 14 dart 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()                                                
                ^^^^^^     
Run Code Online (Sandbox Code Playgroud)

主要.dart:

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()                                                
                ^^^^^^     
Run Code Online (Sandbox Code Playgroud)

答案.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()
              ],
            )));
  }
}

Run Code Online (Sandbox Code Playgroud)

我使用了相同的类名并将正确的文件导入到 main.dart 中。我不确定出了什么问题。有人可以向我指出吗?提前致谢!

Asm*_*oun 8

我不建议您自己编写导入,当您有一些未导入的小部件或函数时,将突出显示错误并指出修复该问题的建议。这是一个例子:

  • 调用你的函数或小部件,这里我有一个小部件要调用:
    调用名为:ScreenLogin() 的小部件

  • 如您所见,错误被突出显示,建议导入

  • 单击该按钮即可正确导入小部件
    导入的小部件

这将防止您将来出现此类错误


小智 8

我有类似的问题。尽管我的导入声明是正确的,但我遇到了这个问题。

然后我做了“flutter clean”,在“flutter run”之后它起作用了。

因此,只需使用“flutter clean”清理构建即可查看。希望它有效。

  • 这个解决方案对我没有帮助 (3认同)

Oma*_*att 1

如果没有完整的最小重现和重现行为的步骤,我无法复制您收到的相同错误。import 'answer.dart';在我的测试中,在 main.dart 中手动添加并通过热重载运行应用程序时发生错误。由于它是热重载并且导入是手动添加的,因此很可能未包含代码更改,从而导致了该问题。重新启动后应用程序工作正常,并且当您让 IDE 自动完成导入时不会出现该问题。\xc2\xa0

\n

根据目前的详细信息,我无法排除导致您的问题的任何明确原因。

\n

我正在 Flutter 稳定通道版本 1.22.0 上运行

\n

我得到的错误:

\n
\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90 Exception caught by widgets library \xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\nThe following _CompileTimeError was thrown building MyApp(dirty, state: _MyAppState#2340f):\nUnimplemented handling of missing static target\n\nThe relevant error-causing widget was: \n\xc2\xa0 MyApp file:///Users/{USER}/Downloads/dev/flutter/sample60384439/lib/main.dart:5:23\nWhen the exception was thrown, this was the stack: \n#0 \xc2\xa0 \xc2\xa0 \xc2\xa0StatefulElement.build (package:flutter/src/widgets/framework.dart:4744:28)\n#1 \xc2\xa0 \xc2\xa0 \xc2\xa0ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4627:15)\n#2 \xc2\xa0 \xc2\xa0 \xc2\xa0StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4800:11)\n#3 \xc2\xa0 \xc2\xa0 \xc2\xa0Element.rebuild (package:flutter/src/widgets/framework.dart:4343:5)\n#4 \xc2\xa0 \xc2\xa0 \xc2\xa0BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2730:33)\n...\n\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\nReloaded 2 of 530 libraries in 220ms.\n
Run Code Online (Sandbox Code Playgroud)\n


归档时间:

查看次数:

83749 次

最近记录:

2 年,11 月 前