我是 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 中。我不确定出了什么问题。有人可以给我指出来。提前致谢!
最佳答案
我不建议您自己编写导入,当您有一些未导入的小部件或函数时,将突出显示错误并指示修复该问题的建议。
这是一个例子:
这将防止您将来出现此类错误
关于flutter - 该方法未为类定义 - Flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60384439/