flutterflutter-dependenciesflutter-test

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 ?


Solution

  • 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.

    1. issuecomment
    2. issuecomment
    3. issuecomment

    An alternative to flutter run -t lib/main_dev.dart in VS Code with the debugger tool.

    .vscode/launch.json

      "configurations": [
    
        {
          "name": "Flutter",
          "request": "launch",
          "type": "dart",
          // "args": ["--enable-software-rendering"]
          // "flutterMode": "profile", //debug //release 
          "program": "${workspaceFolder}/lib/main_dev.dart"
        }
      ]