【问题标题】:The method isn't defined for the class - Flutter该方法未为该类定义 - Flutter
【发布时间】: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()                                                
                ^^^^^^     

ma​​in.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 会有所帮助。但为什么呢?

标签: flutter dart


【解决方案1】:

我尝试了上面给出的所有解决方案,但没有任何效果。这是 Flutter SDK 和 Dart 版本的问题。最后升级了flutter sdk,就成功了。这是在这里建议的GitHub solution

flutter upgrade --force

【讨论】:

    【解决方案2】:

    我遇到了类似的问题。即使我的导入声明是正确的,我也遇到了这个问题。

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

    所以,只需使用“flutter clean”清理构建并查看。希望它有效。

    【讨论】:

    • 这个解决方案对我没有帮助
    【解决方案3】:

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

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

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

    • 单击然后小部件正确导入

    这将在将来为您防止此类错误

    【讨论】:

      【解决方案4】:

      不一样但类似的错误。我做了热重启而不是热重载。 注意:Windows 10、Android Studio IDE

      【讨论】:

      • 我推荐你使用像grammarly.com这样的语法检查器
      【解决方案5】:

      如果没有完整的最小重现和重现该行为的步骤,我将无法重现您收到的相同错误。在我的测试中,在 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.
      

      【讨论】:

        猜你喜欢
        • 2022-11-03
        • 1970-01-01
        • 1970-01-01
        • 2012-08-07
        • 2013-09-07
        • 1970-01-01
        • 1970-01-01
        • 2021-09-02
        • 2020-09-02
        相关资源
        最近更新 更多