You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行flutter run时提示"Target file "lib/main.dart" not found"的原因及解决方法

Hey there! Let's break down why you're seeing the Target file "lib/main.dart" not found error when running flutter run, and walk through how to fix it step by step.

Common Causes

  • You're not in the correct project directory:This is the most frequent reason. If your terminal is open in a folder that's not the root of your Flutter project (like the parent folder, or a subdirectory such as android/ or ios/), Flutter can't locate the default entry file it expects.
  • lib/main.dart was deleted or renamed:Maybe you accidentally removed this default starting file, or renamed it to something else (like main_dev.dart). Flutter looks for lib/main.dart as the default launch point out of the box.
  • Your project structure was modified:If you moved the entry file to a subfolder (e.g., lib/src/main.dart) without updating Flutter's target path, the command will fail to find it.
  • Incomplete project initialization:Sometimes, when creating a project with flutter create, the process might be interrupted, leaving you without a lib directory or main.dart file.

Fixes to Try

1. Confirm your terminal's current directory

First, check where you're running the command from:

  • On Linux/macOS, run pwd to view your current path.
  • On Windows, run cd (without any arguments) to see your current directory.

Your path should point to the root of your Flutter project—this is the folder containing pubspec.yaml, lib/, android/, and ios/ folders. If you're not there, use the cd command to navigate to it, for example:

cd ~/Documents/MyFlutterApp

Then re-run flutter run.

2. Check if lib/main.dart exists

Open your project folder and look inside the lib directory for main.dart:

  • If it's missing, create a basic version to get started:
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(title: const Text('Hello Flutter')),
            body: const Center(child: Text('Error fixed!')),
          ),
        );
      }
    }
    
  • If you renamed it, either change it back to main.dart, or run the command with the new file path:
    flutter run lib/your_custom_main_file.dart
    

3. Specify a custom entry file path

If your entry file is in a non-default location (like lib/src/app.dart), directly tell Flutter where to find it in the run command:

flutter run lib/src/app.dart

4. Reinitialize the project (if initialization failed)

If you suspect the project wasn't created properly, first back up any important files. Then you can either:

  • Delete the incomplete project and create a new one with flutter create my_new_project, then move your existing files over.
  • Run flutter clean to clear cached files, followed by flutter pub get to reinstall dependencies, and check if lib/main.dart appears.

内容的提问来源于stack exchange,提问作者Keshav Aditya R P

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:40:02