我是新手,一直在尝试制作一个简单的测验应用程序。我遇到了一个错误,我不确定是什么错误。
错误:
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:
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:
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中。我不知道怎么了。有人可以向我指出。预先感谢!
答案 0 :(得分:0)
如果没有完整的最小程序复制和重现此行为的步骤,我将无法复制您收到的相同错误。在我的测试中,在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.
答案 1 :(得分:0)
不一样,但类似的错误。我执行了热重启,而不是热重新加载。 注意:Windows 10,Android Studio IDE
答案 2 :(得分:0)
我不建议您自己编写导入,当您有一些未导入的小部件或函数时,将突出显示错误并指出修复建议。 这是一个例子:
这将在以后为您避免此类错误
答案 3 :(得分:0)
我遇到了类似的问题。即使我的导入语句是正确的,我也遇到了这个问题。
然后我做了“flutter clean”并且在“flutter run”之后它起作用了。
因此,只需使用“flutter clean”清理构建并查看。希望它有效。