[英]Flutter : Target file "lib/main.dart" not found
When I perform a flutter run I get an error当我执行flutter 运行时出现错误
Target file "lib/main.dart" not found.找不到目标文件“lib/main.dart”。
Why is this happening and how can I fix this?为什么会发生这种情况,我该如何解决?
You can run any file from any DIR provided that you set the target file path, example:您可以从任何 DIR 运行任何文件,前提是您设置了目标文件路径,例如:
flutter run -t lib/main_dev.dart
OR或者
flutter run lib/dev/main_dev.dart
UPDATE (2020 February 5th)更新(2020年2月5日)
It is however not advisable to remove main.dart from your project.但是,不建议从项目中删除main.dart 。
I'm sure most of you found this link because you are setting up / configuring your app to accommodate for different environments eg dev, stg, beta and prod.我相信你们中的大多数人都找到了这个链接,因为您正在设置/配置您的应用程序以适应不同的环境,例如 dev、stg、beta 和 prod。
Example:例子:
main_dev.dart: main_dev.dart:
void main() async {
dynamic configuredApp = AppConfig(
appName: 'Flutter',
flavorName: 'development',
appVersion: 1.0,
apiBaseUrl: 'https://dev-api.example.com/'
);
runApp(App(configuredApp));
}
main.dart main.dart
class App extends StatefulWidget {
final dynamic configuredApp;
App(this.configuredApp);
@override
_AppState createState() => _AppState();
}
As it turns out some build steps will fail in Android Studio mostly Gradle related if you don't have a main.dart file and method main() {} referenced inside this file.事实证明,如果您没有在此文件中引用 main.dart 文件和方法 main() {},则 Android Studio 中的一些构建步骤将失败,主要与 Gradle 相关。
More info / solutions below related to flutter build error: finished with non-zero exit value 1以下与颤振构建错误相关的更多信息/解决方案:以非零退出值 1 结束
An alternative to flutter run -t lib/main_dev.dart
in VS Code with the debugger tool.使用调试器工具在 VS Code 中替代
flutter run -t lib/main_dev.dart
。
.vscode/launch.json .vscode/launch.json
"configurations": [
{
"name": "Flutter",
"request": "launch",
"type": "dart",
// "args": ["--enable-software-rendering"]
// "flutterMode": "profile", //debug //release
"program": "${workspaceFolder}/lib/main_dev.dart"
}
]
Hope this helps.希望这可以帮助。
Flutter is looking for main.dart
in lib
folder while you must have had file inside any other package. Flutter 正在
lib
文件夹中查找main.dart
,而您必须在任何其他包中都有文件。
Best solution is to place your main.dart
file just inside lib
folder.最好的解决方案是将
main.dart
文件放在lib
文件夹中。 Flutter run command will work then for sure. Flutter run 命令肯定会起作用。
It worked for me.它对我有用。
main.dart
url should be: main.dart
网址应该是:
<app dir>/lib/main.dart
If you are using vscode...如果您使用的是 vscode...
When the error occurs, the vscode folder occurs in the side bar.出现错误时,vscode文件夹出现在侧边栏中。 It has the launch.json file inside.
它里面有launch.json文件。
You can change the "program": "..." property in the launch.json file to run the .dart file in the directory where you want.您可以更改 launch.json 文件中的"program": "..."属性,以在所需目录中运行 .dart 文件。
This happened when I used Visual Studio Code.这发生在我使用 Visual Studio Code 时。 What I did to fix the problem was by editing the
.vscode/launch.json
file and change the value of "program"
to the absolute path of my main.dart
file (eg C://...
).我为解决这个问题所做的是通过编辑
.vscode/launch.json
文件并将"program"
的值更改为我的main.dart
文件的绝对路径(例如C://...
)。
So basically when you execute this所以基本上当你执行这个
flutter run
Flutter tries to find main.dart
in /lib
directory. Flutter 尝试在
/lib
目录中找到main.dart
。
So if you have a file that is present in some other child directory of /lib
directory or has a different name like result_page.dart
, you'd encounter this error.因此,如果您的文件存在于
/lib
目录的其他子目录中或具有不同的名称,例如result_page.dart
,您会遇到此错误。 Now, a lot of answers had suggested to either rename your file to main.dart
or move it directly under /lib
directory.现在,很多答案都建议将您的文件重命名为
main.dart
或将其直接移动到/lib
目录下。
But you can actually simply run any file by simply providing the target file's path from the current directory like this -但是您实际上可以通过简单地从当前目录提供目标文件的路径来简单地运行任何文件,如下所示 -
flutter run --target=lib/customization/result_screen.dart
So this would simply execute my result_screen.dart
file present under some other child directory called customization
.因此,这将只是执行我的
result_screen.dart
叫一些其他子目录下的文件,目前customization
。
You can also generate the APK file following the same logic and the command would look like this -您还可以按照相同的逻辑生成 APK 文件,命令如下所示 -
flutter build apk --target=lib/customization/result_screen.dart --target-platform=android-arm64
where --target-platform=android-arm64
is optional and is meant for 64-bit Android devices which helps in reducing APK size which otherwise would generate a fat APK including both 32-bit and 64-bit binaries.其中
--target-platform=android-arm64
是可选的,适用于 64 位 Android 设备,这有助于减少 APK 大小,否则会生成包含 32 位和 64 位二进制文件的胖 APK。 More info here更多信息在这里
If you are using visual code and you encounter this problem while debugging, just go to launch.json, go to the location of where your flutter app is located, to the lib folder and then to the main.dart ,copy the path then add it to the text program in the launch.json as indicated on the photo below如果你使用的是可视化代码并且在调试时遇到这个问题,只需转到launch.json,转到你的flutter应用程序所在的位置,到lib文件夹然后到main.dart,复制路径然后添加如下图所示,将其添加到launch.json中的文本程序
VSCode 用户:请导航到 launch.json 并将“Program”行替换为以下内容:
"Program":"${rootProject.buildDir}/${project.name}/lib/main.dart"
in android studio this wokrs for me在 android studio 中,这对我来说是可行的
this executes an snapshot aplicaction to configure the root profile a run your app这将执行快照应用程序以配置根配置文件并运行您的应用程序
If you are using the Debug Mode
in vs code
, ensure that the vs code
working folder is your Flutter
project main folder.如果您在
vs code
中使用Debug Mode
,请确保vs code
工作文件夹是您的Flutter
项目主文件夹。
If you want to change the vs code
working folder, you can use the File > Open Folder
menu or Ctrl+O
shortcut.如果要更改
vs code
工作文件夹,可以使用“ File > Open Folder
菜单或Ctrl+O
快捷键。
When I change the folder, It worked for me .当我更改文件夹时,它对我有用。
What my case was I forgot to write code for calling main....我的情况是我忘记编写调用 main 的代码......
void main()
{
runApp......
}
And doing this worked这样做有效
Try final FlutterDevice flutterDevice = await FlutterDevice.create
尝试
final FlutterDevice flutterDevice = await FlutterDevice.create
The root cause is that the ResidentRunner we use to attach and do hot reloads will try to find a main.dart
file when a target isn't announced.根本原因是我们用来附加和热重载的ResidentRunner会在目标未宣布时尝试查找
main.dart
文件。 Attach doesn't provide a target, so it uses the default behavior always. Attach 不提供目标,因此它始终使用默认行为。 As per DaceShuckerow .
根据DaceShuckerow 。
This issue is still there on the official Repo .这个问题仍然存在于官方Repo 上。
Check folder .vscode>launch.json and add configuration:检查文件夹.vscode>launch.json并添加配置:
if you have it already change a "program": "path of main.dart example: lib/main.dart"如果您已经更改了“程序”:“main.dart 示例的路径:lib/main.dart”
{ "name": "Dart", "type": "dart", "request": "launch", "program": "lib/main.dart" },
If you are using VSCode
then try this method :如果您使用的是
VSCode
尝试以下方法:
Delete .vscode folder in your project directory and rerun the program.删除项目目录中的.vscode文件夹并重新运行程序。
If you are having multiple iOS schemes then you could try like this:如果您有多个 iOS 方案,那么您可以尝试这样:
flutter run --flavor UserDev -t lib/user/dev/main_dev.dart
For,为了,
"configurations": [{
"name": "User-Dev",
"request": "launch",
"type": "dart",
"program": "lib/user/dev/main_dev.dart",
"args": [
"--flavor",
"UserDev"
]
}]
In case someone is struggling with this what worked for me is to recreate ios folder.如果有人为此苦苦挣扎,对我有用的是重新创建 ios 文件夹。 And -t works regardless of the location of the target or name of the target and without any need to have main.dart in your lib folder.
无论目标的位置或目标的名称如何, -t 都可以工作,并且不需要在 lib 文件夹中包含 main.dart。
To recreate ios folder重新创建ios文件夹
我有一个类似的错误信息,这是因为我不小心删除了 void main()
I think you already change the path of main.dart.If so You can drag and drop the main.dart file to lib folder.(lib/main.dart)我认为您已经更改了 main.dart 的路径。如果是这样,您可以将 main.dart 文件拖放到 lib 文件夹中。(lib/main.dart)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.