【发布时间】:2020-06-08 14:04:07
【问题描述】:
我是 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()
^^^^^^
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。我不确定出了什么问题。有人可以向我指出。提前致谢!
【问题讨论】:
-
您是否让您的 IDE 自动完成导入?你试过
import 'answer.dart';(没有前导./)吗?你重启 Flutter 进程了吗? -
@FrankTreacy 谢谢,我重新启动了颤振过程并且它工作了。
-
对,我遇到了确切的问题,重新启动 Flutter 会有所帮助。但为什么呢?