Keshav Aditya R P
Keshav Aditya R P

Reputation: 3221

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

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 ?

Upvotes: 70

Views: 142321

Answers (21)

UCHE CHINEDU
UCHE CHINEDU

Reputation: 1

In my own case, it was a trailing space in the lib folder. like this lib /main instead of lib/main You should probably double check if you encounter this issue.

Upvotes: 0

zinxer
zinxer

Reputation: 1

lib/Screen/SplashScreen/Splash.dart:10:8: Error: Error when reading 'lib/widgets/applogo.dart': No such file or directory import 'package:eshop_multivendor/widgets/applogo.dart'; ^ lib/Screen/Auth/Login.dart:12:8: Error: Error when reading 'lib/widgets/applogo.dart': No such file or directory import 'package:eshop_multivendor/widgets/applogo.dart'; ^ lib/Screen/Auth/SignInUpAcc.dart:3:8: Error: Error when reading 'lib/widgets/applogo.dart': No such file or directory import 'package:eshop_multivendor/widgets/applogo.dart'; ^ lib/Screen/Auth/SendOtp.dart:5:8: Error: Error when reading 'lib/widgets/applogo.dart': No such file or directory import 'package:eshop_multivendor/widgets/applogo.dart'; ^ lib/Screen/Auth/Verify_Otp.dart:8:8: Error: Error when reading 'lib/widgets/applogo.dart': No such file or directory import 'package:eshop_multivendor/widgets/applogo.dart'; ^ lib/Screen/Auth/Set_Password.dart:2:8: Error: Error when reading 'lib/widgets/applogo.dart': No such file or directory import 'package:eshop_multivendor/widgets/applogo.dart'; ^ lib/Screen/Auth/SignUp.dart:8:8: Error: Error when reading 'lib/widgets/applogo.dart': No such file or directory import 'package:eshop_multivendor/widgets/applogo.dart'; ^ lib/Screen/Auth/Login.dart:1099:20: Error: Couldn't find constructor 'AppLogo'. child: const AppLogo(), ^^^^^^^ lib/Screen/Auth/SignInUpAcc.dart:30:20: Error: Couldn't find constructor 'AppLogo'. child: const AppLogo(), ^^^^^^^ lib/Screen/Auth/SendOtp.dart:696:20: Error: Couldn't find constructor 'AppLogo'. child: const AppLogo(), ^^^^^^^ lib/Screen/Auth/Verify_Otp.dart:285:20: Error: Couldn't find constructor 'AppLogo'. child: const AppLogo(), ^^^^^^^ lib/Screen/Auth/Set_Password.dart:393:20: Error: Couldn't find constructor 'AppLogo'. child: const AppLogo(), ^^^^^^^ lib/Screen/Auth/SignUp.dart:562:20: Error: Couldn't find constructor 'AppLogo'. child: const AppLogo(), ^^^^^^^ Target kernel_snapshot failed: Exception

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ':app:compileFlutterBuildDebug'.

Process 'command '/home/user/snap/flutter/common/flutter/bin/flutter'' 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.

BUILD FAILED in 16s Running Gradle task 'assembleDebug'... 16.6s Error: Gradle task assembleDebug failed with exit code 1

Upvotes: 0

Adam
Adam

Reputation: 39

you probably just changed location of main.dart try running : flutter run yourPath/main.dart or whatever you named it

Upvotes: 0

Elroy
Elroy

Reputation: 1670

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
  1. issuecomment
  2. issuecomment
  3. 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"
    }
  ]

Upvotes: 115

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)

Upvotes: 0

Mina Farid
Mina Farid

Reputation: 5887

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

enter image description here

Upvotes: 3

Salah Qamachi
Salah Qamachi

Reputation: 33

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

Upvotes: 1

Ali_R
Ali_R

Reputation: 121

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

Upvotes: 0

Zaid Pathan
Zaid Pathan

Reputation: 16820

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"
            ]
        }]

Upvotes: 2

Munish Thakur
Munish Thakur

Reputation: 996

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

Upvotes: 17

Shubhral
Shubhral

Reputation: 334

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

Upvotes: 9

Augustine Aykara
Augustine Aykara

Reputation: 179

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

Upvotes: 0

Aminjoni Abdullozoda
Aminjoni Abdullozoda

Reputation: 61

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"
},

Upvotes: 3

SanjaySingh
SanjaySingh

Reputation: 1857

What my case was I forgot to write code for calling main....

void main()
{
  runApp......
}

And doing this worked

Upvotes: 0

champion-runner
champion-runner

Reputation: 1647

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.

Upvotes: 0

Mehmet B&#252;t&#252;n
Mehmet B&#252;t&#252;n

Reputation: 749

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.

Upvotes: 0

in android studio this wokrs for me

  1. left click in main.dart
  2. right click in profile main.dart

this executes an snapshot aplicaction to configure the root profile a run your app

enter image description here

Upvotes: 1

Sukhi
Sukhi

Reputation: 14145

VSCode Users: Plese navigate to launch.json and replace the "Program" line with the following :

"Program":"${rootProject.buildDir}/${project.name}/lib/main.dart"

Upvotes: 0

Kefa Mutinda
Kefa Mutinda

Reputation: 51

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

Upvotes: 2

Steve Lukis
Steve Lukis

Reputation: 459

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://...).

Upvotes: 3

NewbieCoder
NewbieCoder

Reputation: 71

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.

Upvotes: 3

Related Questions