Flutter : Target file "lib/main.dart" not found - flutter

When I perform a flutter run I get an error
Target file "lib/main.dart" not found.
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:
flutter run -t lib/main_dev.dart
OR
flutter run lib/dev/main_dev.dart
UPDATE (2020 February 5th)
It is however not advisable to remove main.dart from your project.
I'm sure most of you found this link because you are setting up / configuring your app to accommodate for different environments e.g. dev, stg, beta and prod.
Example:
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
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.
Common build errors (AndroidX migrate, APK build etc.)
More info / solutions below related to flutter build error: finished with non-zero exit value 1
issuecomment
issuecomment
issuecomment
An alternative to flutter run -t lib/main_dev.dart
in VS Code with the debugger tool.
Click "Add Configuration" and add the following or add manually:
.vscode/launch.json
"configurations": [
{
"name": "Flutter",
"request": "launch",
"type": "dart",
// "args": ["--enable-software-rendering"]
// "flutterMode": "profile", //debug //release
"program": "${workspaceFolder}/lib/main_dev.dart"
}
]

Flutter is looking for main.dart in lib folder while you must have had file inside any other package.
Best solution is to place your main.dart file just inside lib folder. Flutter run command will work then for sure.
It worked for me.
main.dart url should be:
<app dir>/lib/main.dart

So basically when you execute this
flutter run
Flutter tries to find main.dart in /lib directory.
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.
Now, a lot of answers had suggested to either rename your file to main.dart or move it directly under /lib directory.
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.
You can also generate the APK file following the same logic and the command would look like this -
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. More info here

If you are using vscode...
When the error occurs, the vscode folder occurs in the side bar. It has the launch.json file inside.
You can change the "program": "..." property in the launch.json file to run the .dart file in the directory where you want.

This happened when I used 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 (e.g. C://...).

If you are using VSCODE
Check folder .vscode>launch.json and add configuration:
if you have it already change a "program": "path of main.dart example: lib/main.dart"
{
"name": "Dart",
"type": "dart",
"request": "launch",
"program": "lib/main.dart"
},

Make Sure that your main.dart inside lib folder directly not in another package or directory

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 "program" path

in android studio this wokrs for me
left click in main.dart
right click in profile main.dart
this executes an snapshot aplicaction to configure the root profile a run your app

If you are having multiple iOS schemes then you could try like this:
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"
]
}]

I had a similar error message, and it happened because I accidentally deleted the void main()

VSCode Users: Plese navigate to launch.json and replace the "Program" line with the following :
"Program":"${rootProject.buildDir}/${project.name}/lib/main.dart"

If you are using the Debug Mode in vs code, ensure that the vs code working folder is your Flutter project main folder.
If you want to change the vs code working folder, you can use the File > Open Folder menu or Ctrl+O shortcut.
When I change the folder, It worked for me.

Try 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. Attach doesn't provide a target, so it uses the default behavior always. As per DaceShuckerow.
This issue is still there on the official Repo.

What my case was
I forgot to write code for calling main....
void main()
{
runApp......
}
And doing this worked

In case someone is struggling with this what worked for me is to recreate ios folder. 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.
To recreate ios folder
rename ios folder to something like ios_old
run => flutter create fakeApp
open fakeApp, copy the ios folder to your project
migrate changes from ios_old (if you had any changes)
delete ios_old

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)

If you are using VSCode then try this method :
Delete .vscode folder in your project directory and rerun the program.

Related

How do I change the working directory for debugging in VS Code + CMakeTools

I have a VS Code project based on a CMakeLists.txt file, and using the CMakeTools extension I can successfully build the target executable.
However, I need to run the executable in a different directory to the build directory. I cannot find a setting to either:
The built executable is placed in a different directory to the build directory and then run from there
The build executable is run from a different working directory
How can I achieve my goal?
You can change the output directory for the executable using the RUNTIME_OUTPUT_DIRECTORY property. When debugging, the executable is run in that directory. For example:
set_target_properties(my_target
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
)
If you only want to change the current working directory (cwd), you can create your custom .vscode/launch.json. The content may depend on your OS and compiler:
{
"version": "0.2.0",
"configurations": [
{
"name": "Run CMake Target",
"type": "cppvsdbg", // use cppdbg on linux
"request": "launch",
"program": "${command:cmake.launchTargetPath}",
"cwd": "${workspaceFolder}/bin"
}
]
}

Flutter: different app icon and app name depending on release/production build or debug/development build?

Is there any way of changing the app's name and/or the app's icon depending on the building mode selected (development or production) ?
release mode -> Logo A & <app_name>
debug mode -> Logo B & <app_name>-dev
I've been looking but could't find anything —or didn't search at the right place. Can anyone help ?
If you're trying to build an Android application, there are several steps that you should follow
Please, note that if you want to keep both applications in one device, you should change the package name of the app, that can be done easily by adding a suffix inside the app's level build.gradle projectName/android/app/build.gradle a suffix for the build type of the debug mode like this:
buildTypes {
release {
signingConfig signingConfigs.release
}
debug {
applicationIdSuffix ".debug"
}
}
For the app's name:
In your project's folder there is a folder named android/app/src, inside it, you will find three more folders
Move inside this debug directory and add a folder named res, in there you will be able to store the resources that will be used in development(debug) mode and inside it create a directory named values
Create a file named strings.xml, in this folder, you can store your dynamic app's name
Here you will define your app's name, copy inside it the code below:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">app_name-DEV</string>
</resources>
Also you will have to add the string.xml file in the same path in the main folder(below the debug folder) inside the same path main/res/values inside it, you should apply a different name for the key app_name like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">app_name-DEV</string>
</resources>
in the main folder, there is a file named: AndroidManifest.xml, inside it remove the line android:name="${applicationName}" because it will cause few conficts and change the line: android:label:"some_label" -> android:label="#string/app_name"
From this file, copy the whole tag, and paste it inside the debug's folder AndroidManifest.xml and before the line android:label="#string/app_name" add this line tools:replace="android:label", this will enable the debug's manifest to override the app's name.
-> projectFolder/android/app/src/debug/AndroidManifest.xml
For the app's icon:
in the projectFolder/android/app/src/debug/res you should create the mipmap folders(you can copy them from the projectFolder/android/app/src/main/res folder and then to modify the icons)
inside them, you should add a the icon for the development mode with the same, for this purpose, you can follow the following package flutter_launcer_icon (Please make sure that in the end you will run this command: flutter pub run flutter_launcher_icons:main , but still you can add them manually)
That's all.
Hope I was enough thorough :)
If you have any questions, let me know
Eventually, I used a flutter feature called flavors which works for Android and iOS.
Here is the guide I followed : https://youtu.be/Vhm1Cv2uPko
For devs using VS Code, I created a VS Code config file to make launching the apps in the different flavors easier :
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Development",
"request": "launch",
"type": "dart",
"program": "lib/main_dev.dart",
"args" : ["--flavor", "development", "--target", "lib/main_dev.dart"],
// ? Keep flutterMode
"flutterMode": "debug",
},
{
"name": "Launch Production",
"request": "launch",
"type": "dart",
"program": "lib/main_prod.dart",
"args": ["--flavor", "production", "--target", "lib/main_prod.dart"],
// ? Keep flutterMode
"flutterMode": "release",
},
{
"name": "Run All Tests",
"type": "dart",
"request": "launch",
"program": "./test/",
},
]
}
This subject is treated in the video linked.

java.lang.ClassNotFoundException: Cannot find implementation for MyMapper in Visual studio code

Class not found exception in Visual studio code because vs-code debug takes output class files form bin directory like eclipse but i am using GRADLE for build so MapperImpl.java which is implementation files in mapper are created in the build folder i don't know how to solve the issue i tried to change the classpath all the solutions i tried but nothing working please i need your help
I am also posting the vs code file launch.json
{
"type": "java",
"name": "Debug (Launch)-Application<RestApi>",
"request": "launch",
"mainClass": "ae.org.nge.Application",
"projectName": "RestApi",
"vmArgs": "-DngeConfig=C:/Users/Manoj.Dhayalan/source/myapp/Application/config/myapp-config.properties -Dserver.port=8081 -Dspring.profiles.active=dev"
}
I don't use Visual Studio Code but I managed to change the location of generated class file by putting this lines into my build.gradle file:
compileJava {
options.setAnnotationProcessorGeneratedSourcesDirectory(file("$projectDir/src/main/generated"))
}
This way gradle will generate MyMapperImpl in src instead of build folder and it should be also recognized by VS Code.

Hide typescript files in ido-find-file when javascript files are present in folder Emacs

Whenever I use ido-find-buffer in Emacs I most of the time get the processed javascript files as first option, while I'd much rather get typescript files first.
Then again, I do not want to always hide javascript files, I guess only when using typescript as well.
Perhaps the best is to have typescript put the javascript files in another folder itself.
Is there a good solution for this?
You can have Typescript store the compiled files in another folder using the compiler option
"outDir": "dist"
Or whatever folder you want them to output into
I just noticed it is possible to define it in package.json:
{
"name": "app",
"version": "0.1",
"scripts": {
"tsc": "tsc",
"tsc:w": "tsc -w --outDir build", // <--- here
"lite": "lite-server",
"start": "concurrent \"npm run tsc:w\" \"npm run lite\" "
},
...
It gets written to a build directory and thus is not causing issues anymore.

Ionic serve ignoring gulpStartupTasks

I have this ionic.project file:
{
"name": "foobar",
"app_id": "com.foo.bar",
"gulpStartupTasks": [
"styles",
"source",
"watch"
],
"watchPatterns": [
"www/**/*",
"!www/lib/**/*"
],
"sourceModules": {
"ionic": "git://github.com/driftyco/ionic.git",
"ng-cordova": "git://github.com/driftyco/ng-cordova.git"
}
}
But the gulp tasks are not being executed, I even added some console.logs to debug but nothing happened.
Any ideas?
UPDATE:
I've detected that the gulpStartupTasks are being executed asynchronously with the Ionic initialization, so when Ionic tries to find the www folder and don't find it (because my startup tasks haven't run yet) it fails and kill the process
But if I create an empty www folder to trick Ionic it works but opens a browser with an error saying that the index.html haven't been found
However, some seconds after that I see the startup tasks being executed in my shell
And if I refresh the page it works
How can I make these startup tasks run before ionic tries to find the www folder?
According to the latest Ionic-Cli documentation, if you want any gulp tasks to run before the app is served, add a serve:before task to the gulpfile.js file in your project root. In your case this would be:
gulp.task("serve:before", [
"styles",
"source",
"watch"
]);
I figured that Ionic wasn't ignoring gulpStartupTasks as I've previously updated the question, but rather executing them asynchronously to the server initialization.
To fix that I've added a postinstall script in my package.json to do the job of creating the www folder, processing the source files and then copying them to the www folder.
That solved the problem, but I still don't understand why gulpStartupTasks execute async instead of sync, it seems to be wrong. Don't you?
So I figured this out. I'm guessing that you, like myself, are editing in a different .sass or .scss file than the one that comes with ionic skeleton apps. In this case you need to add this new path to the gulp file or livereload will pick up the changes, but not actually perform the 'sass' command on the path with your new SASS file(s).
Here's my edited paths line in gulpfile.js
var paths = {
sass: ['./scss/**/*.scss', './www/sass/**/*']
};
'./www/sass/**/*' is where I put my new SASS files that I #import in the main ionic.app.scss file.
// Include all of Ionic
#import "www/lib/ionic/scss/ionic";
#import "www/sass/test";