Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 580
Keshav Aditya R.P Avatar asked May 25 '18 05:05

Keshav Aditya R.P


People also ask

Where is Main Dart file in Flutter?

Once you have created your project, you will see the “lib” folder where the “main. dart” file is located. Most importantly, once you have created a project and developed the application, Flutter expects the “main. dart” file to exist.

How do you open the main file in darts?

2- The first Dart example with Android Studio First, create the MyFirstDartProject folder. Secondly, on Android Studio, open the folder you have just created. Then create a new file: File > New > File > MyFirstDart.

How do you run a release Flutter?

To compile in release mode, we just need to add the --release flag to the flutter run command and have a physical device connected. Although we can do so, we typically do not use the flutter run command with the --release flag.


1 Answers

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

Hope this helps.

like image 169
Elroy Avatar answered Sep 19 '22 06:09

Elroy