flutter - 该方法未为类定义 - Flutter

标签 flutter 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()                                                
                ^^^^^^     
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 中。我不确定出了什么问题。有人可以给我指出来。提前致谢!

最佳答案

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

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

    calling a widget named : ScreenLogin()
  • 如您所见,错误突出显示,建议导入
  • 单击然后小部件正确导入

    imported widget

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

    关于flutter - 该方法未为类定义 - Flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60384439/

    相关文章:

    javascript - Client-Server 模型,使用 Dart 作为 Client,Go 语言作为 Server

    firebase-realtime-database - 如何在 Dart 2 中为我的用例键入 cast

    flutter - 删除 ROw 中两个 IconButton 之间的多余空间

    flutter - 为什么这个图标被剪掉了?

    Flutter:StreamProvider 的奇怪行为,使用不完整数据重建的小部件

    android - 应用程序分析/性能测试

    dart - 在网络调用后完成补全器时,不会调用分配给我的Compler.future对象的回调

    firebase - Flutter Firebase 错误(任务 ':app:mergeExtDexDebug' 执行失败)

    dart:将类构造函数标记为const的含义是什么

    dart - 未处理的异常 : NoSuchMethodError: The method 'toDate' was called on null