如何修复颤振 lib/main.dart 错误
Posted
技术标签:
【中文标题】如何修复颤振 lib/main.dart 错误【英文标题】:How to fix flutter lib/main.dart errors 【发布时间】:2021-04-08 11:31:39 【问题描述】:我是 Flutter 的初学者,这是我的第一个项目。我使用命令 flutter create 设置了一个新项目,然后我遇到了多个问题,我使用flutter doctor -v
修复了这些问题,但现在我遇到了这些错误,我似乎无法找到解决它们的方法,每次我运行颤振运行 Running Gradle task 'assembleDebug'... 需要很长时间,然后出现以下错误:
Launching lib\main.dart on sdk gphone x86 in debug mode...
Running Gradle task 'assembleDebug'...
lib/main.dart:7:37: Error: Can't find '' to match ''.
class MyApp extends StatelessWidget
^
lib/main.dart:33:1: Error: Classes can't be declared inside other classes.
Try moving the class to the top-level.
class MyHomePage extends StatefulWidget
^^^^^
lib/main.dart:33:18: Error: Expected a class member, but got 'extends'.
class MyHomePage extends StatefulWidget
^^^^^^^
lib/main.dart:33:26: Error: A method declaration needs an explicit list of parameters.
Try adding a parameter list to the method declaration.
class MyHomePage extends StatefulWidget
^^^^^^^^^^^^^^
lib/main.dart:51:1: Error: Classes can't be declared inside other classes.
Try moving the class to the top-level.
class _MyHomePageState extends State<MyHomePage>
^^^^^
lib/main.dart:51:24: Error: Expected a class member, but got 'extends'.
class _MyHomePageState extends State<MyHomePage>
^^^^^^^
lib/main.dart:51:32: Error: A method declaration needs an explicit list of parameters.
Try adding a parameter list to the method declaration.
class _MyHomePageState extends State<MyHomePage>
^^^^^
lib/main.dart:29:13: Error: The method 'MyHomePage' isn't defined for the class 'MyApp'.
- 'MyApp' is from 'package:new_app/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing method, or defining a method named 'MyHomePage'.
home: MyHomePage(title: 'Flutter Demo Home Page'),
^^^^^^^^^^
lib/main.dart:34:19: Error: Expected ',' before this.
MyHomePage(Key key, this.title) : super(key: key);
^^^
lib/main.dart:34:35: Error: Expected ';' after this.
MyHomePage(Key key, this.title) : super(key: key);
^
lib/main.dart:34:37: Error: Expected an identifier, but got ':'.
MyHomePage(Key key, this.title) : super(key: key);
^
lib/main.dart:34:37: Error: Expected ';' after this.
MyHomePage(Key key, this.title) : super(key: key);
^
lib/main.dart:34:37: Error: Unexpected token ';'.
MyHomePage(Key key, this.title) : super(key: key);
^
lib/main.dart:34:39: Error: Can't use 'super' as an expression.
To delegate a constructor to a super constructor, put the super call as an initializer.
MyHomePage(Key key, this.title) : super(key: key);
^
lib/main.dart:45:16: Error: The final variable 'title' must be initialized.
Try adding an initializer ('= expression') to the declaration.
final String title;
^^^^^
lib/main.dart:48:3: Error: '_MyHomePageState' isn't a type.
_MyHomePageState createState() => _MyHomePageState();
^^^^^^^^^^^^^^^^
lib/main.dart:34:3: Error: The method 'MyHomePage' isn't defined for the class 'MyApp'.
- 'MyApp' is from 'package:new_app/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing method, or defining a method named 'MyHomePage'.
MyHomePage(Key key, this.title) : super(key: key);
^^^^^^^^^^
lib/main.dart:34:29: Error: The getter 'title' isn't defined for the class 'MyApp'.
- 'MyApp' is from 'package:new_app/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'title'.
MyHomePage(Key key, this.title) : super(key: key);
^^^^^
lib/main.dart:48:37: Error: The method '_MyHomePageState' isn't defined for the class 'MyApp'.
- 'MyApp' is from 'package:new_app/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing method, or defining a method named '_MyHomePageState'.
_MyHomePageState createState() => _MyHomePageState();
^^^^^^^^^^^^^^^^
lib/main.dart:55:5: Error: The method 'setState' isn't defined for the class 'MyApp'.
- 'MyApp' is from 'package:new_app/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing method, or defining a method named 'setState'.
setState(()
^^^^^^^^
lib/main.dart:77:21: Error: The getter 'widget' isn't defined for the class 'MyApp'.
- 'MyApp' is from 'package:new_app/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'widget'.
title: Text(widget.title),
^^^^^^
FAILURE: Build failed with an exception.
* Where:
Script 'I:\Programs\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 904
* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'I:\Programs\flutter\bin\flutter.bat'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 13m 24s
Exception: Gradle task assembleDebug failed with exit code 1
【问题讨论】:
"lib/main.dart:7:37: 错误:找不到 '' 来匹配 ''。" - 您的 IDE 是否显示任何类型的修复?你是否为你的 IDE 安装了 dart 和 Flutter 扩展? 我确实在 android studio 上安装了它们,但即使安装了它们,我也会收到这些错误 X Flutter plugin not installed;这增加了 Flutter 特定的功能。 X Dart 插件未安装;这增加了 Dart 特定的功能。 可以分享main.dart文件吗? 问题出在android studio中的flutter和dart插件中,因为flutter sdk无法识别它,我修复了它并且它有效,我不认为这是问题的原因因为有人告诉我,我可以忽略这个错误 【参考方案1】:我找到了一个解决方案,flutter 和 dart 插件已安装在 android studio 中,但每次我运行 flutter -v 时都会收到未安装的错误,因此我使用以下步骤修复了它
flutter upgrade
flutter config --android-studio-dir="C:\Program Files\Android\Android Studio"
flutter doctor -v
flutter channel beta
flutter upgrade
然后我创建了一个新项目并启动它并且它工作了
【讨论】:
以上是关于如何修复颤振 lib/main.dart 错误的主要内容,如果未能解决你的问题,请参考以下文章