Flutter Project Structure

Understanding the directory structure of a Flutter project is crucial for efficient development.

This section will provide a detailed introduction to the various components of a Flutter project and their purposes.


Project Directory Structure

When you useflutter createAfter creating a new project, you will get the following directory structure:

my_app/
├── android/          # Android 平台相关代码
├── ios/              # iOS 平台相关代码
├── lib/              # Dart 代码目录(主要开发目录)
│   └── main.dart     # 应用入口文件
├── web/              # Web 平台相关代码
├── windows/          # Windows 桌面相关代码
├── macos/            # macOS 桌面相关代码
├── linux/            # Linux 桌面相关代码
├── test/             # 测试代码目录
├── pubspec.yaml      # 项目配置文件
├── pubspec.lock      # 依赖锁定文件(自动生成)
├── analysis_options.yaml  # Dart 分析器配置
└── README.md         # 项目说明文件

Description of Main Directories and Files

lib/ Directory - Core Code Directory

lib/This directory is the main place where you write Dart code. All application logic should be placed here.

File/directoryDescription
main.dartThe application's entry file, containing the main function and the root Widget.
*.dartOther Dart source files, usually organized by functional modules.

android/ directory - Android platform

Contains Android native project files, used for:

  • Configure application name and icon
  • Setting Application Permissions
  • Integrate native SDKs (e.g., Google Play services)

ios/ Directory - iOS Platform

Contains iOS native project files (Xcode project), used for:

  • Configure application name and icon
  • Set iOS-specific configuration
  • Integrate CocoaPods dependencies

test/ Directory - Test Code

Contains the application's test code, usually including:

  • Unit Testing
  • Widget Testing
  • Integration Testing

pubspec.yaml - Project Configuration

pubspec.yamlThe core configuration file of the Flutter project, defining the project name, version, dependencies, etc.

Example: pubspec.yaml Sample

# Project Name
name
: my_app
# Project Version
version
: 1.0.0+1

# Project description
description
: "My first Flutter app"

# Runtime Environment
environment
:
  sdk
: ^3.0.0

# Project Dependencies
dependencies
:
  flutter
:
    sdk
: flutter

  # Add third-party dependencies
  http
: ^1.0.0
  provider
: ^6.0.0

# Dev dependencies (only used during development)
dev_dependencies
:
  flutter_test
:
    sdk
: flutter
  flutter_lints
: ^3.0.0

# Flutter configuration
flutter
:
  uses-material-design
: true

  # Configure resource files
  assets
:
   - images/
    - assets/data.json

Common Fields in pubspec.yaml

FieldsDescription
nameProject name, must use lowercase letters and underscores.
versionProject version number, in the format x.y.z.
descriptionProject Description Text
dependenciesRuntime Dependency Packages
dev_dependenciesPackages needed for development (e.g., testing tools)
flutter.assetsResource files that need to be packaged

Dependency Management

Add dependency

Usageflutter pub addCommand to Add Dependencies:

$ flutter pub add http

Or Manually Editpubspec.yamlThen run:

$ flutter pub get

Common Dependency Commands

CommandDescription
flutter pub getGet All Dependencies
flutter pub add packageAdd dependency
flutter pub remove packageRemove dependencies
flutter pub outdatedCheck for Outdated Dependencies
flutter pub upgradeUpgrade Dependencies to Latest Versions

Recommended Project Structure

As the application scale grows, it is recommended to adopt a clear project structure:

lib/
├── main.dart              # 入口文件
├── app.dart               # 应用配置
├── core/                  # 核心功能
│   ├── constants/         # 常量定义
│   ├── theme/             # 主题配置
│   └── utils/             # 工具类
├── data/                  # 数据层
│   ├── models/            # 数据模型
│   ├── repositories/     # 仓库(数据接口)
│   └── services/          # 网络服务
├── domain/                # 领域层
│   └── entities/          # 实体类
├── presentation/          # 展示层
│   ├── pages/             # 页面
│   ├── widgets/           # 组件
│   └── providers/         # 状态管理
└── routes/                # 路由配置

A good project structure can improve code maintainability. It is recommended to choose an appropriate structure based on the application scale.

other extensions