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

Keshav Aditya R.P picture Keshav Aditya R.P · May 25, 2018 · Viewed 31.3k times · Source

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 ?

Answer

Elroy picture Elroy · Oct 26, 2018

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.