Question #28EasyFlutter Basics

What is pubspec.yaml in flutter

#flutter

Answer

Overview

pubspec.yaml is the configuration file for Flutter/Dart projects. It defines project metadata, dependencies, assets, and build settings. Think of it as

text
package.json
for Node.js or
text
build.gradle
for Android.


File Structure

yaml
name: my_flutter_app
description: A new Flutter project
publish_to: 'none'
version: 1.0.0+1

environment:
  sdk: '>=3.0.0 <4.0.0'

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.2
  http: ^1.1.0
  provider: ^6.0.0

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^2.0.0

flutter:
  uses-material-design: true
  assets:
    - assets/images/
    - assets/icons/
  fonts:
    - family: Roboto
      fonts:
        - asset: fonts/Roboto-Regular.ttf
        - asset: fonts/Roboto-Bold.ttf
          weight: 700

Key Sections

1. Project Metadata

yaml
name: my_app            # Package name (lowercase, underscores)
description: My Flutter app
version: 1.0.0+1        # version_name+build_number
publish_to: 'none'      # Don't publish to pub.dev

Version Format:

  • text
    1.0.0
    = Version name (shown to users)
  • text
    +1
    = Build number (internal)

2. Environment

yaml
environment:
  sdk: '>=3.0.0 <4.0.0'  # Dart SDK version constraint
  flutter: '>=3.10.0'     # Optional: Flutter version constraint

3. Dependencies

Regular Dependencies

yaml
dependencies:
  flutter:
    sdk: flutter           # Flutter SDK
  
  # Pub.dev packages
  http: ^1.1.0            # Latest compatible version
  provider: 6.0.5         # Exact version
  dio: '>=4.0.0 <5.0.0'   # Range
  
  # Git dependencies
  my_package:
    git:
      url: https://github.com/user/repo.git
      ref: main            # Branch/tag/commit
      path: packages/my_package  # Monorepo path
  
  # Local dependencies
  local_package:
    path: ../local_package

Version Constraints:

  • text
    ^1.2.3
    =
    text
    >=1.2.3 <2.0.0
    (compatible)
  • text
    1.2.3
    = Exact version
  • text
    >=1.0.0 <2.0.0
    = Range
  • text
    any
    = Any version (not recommended)

Dev Dependencies

Only used during development, not included in release.

yaml
dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^2.0.0     # Linter
  build_runner: ^2.4.0      # Code generation
  mockito: ^5.4.0           # Testing

4. Flutter Configuration

Assets

yaml
flutter:
  assets:
    - assets/images/logo.png    # Single file
    - assets/images/            # Entire directory
    - assets/icons/
    - data/config.json

Usage in code:

dart
Image.asset('assets/images/logo.png')

Fonts

yaml
flutter:
  fonts:
    - family: Roboto
      fonts:
        - asset: fonts/Roboto-Regular.ttf
        - asset: fonts/Roboto-Italic.ttf
          style: italic
        - asset: fonts/Roboto-Bold.ttf
          weight: 700
    
    - family: CustomFont
      fonts:
        - asset: fonts/CustomFont.ttf

Usage in code:

dart
Text(
  'Hello',
  style: TextStyle(
    fontFamily: 'Roboto',
    fontWeight: FontWeight.bold,
  ),
)

Material Design

yaml
flutter:
  uses-material-design: true  # Include Material icons

Enables:

dart
Icon(Icons.home)  // Material icons

5. Platform-Specific Settings

Android

yaml
flutter:
  # No direct Android config here
  # Use android/app/build.gradle instead

iOS

yaml
flutter:
  # No direct iOS config here
  # Use ios/Runner/Info.plist instead

Common Patterns

Multiple Asset Directories

yaml
flutter:
  assets:
    - assets/images/
    - assets/videos/
    - assets/data/
    - assets/translations/en.json
    - assets/translations/es.json

Multiple Font Families

yaml
flutter:
  fonts:
    - family: OpenSans
      fonts:
        - asset: fonts/OpenSans-Regular.ttf
        - asset: fonts/OpenSans-Bold.ttf
          weight: 700
    - family: Lato
      fonts:
        - asset: fonts/Lato-Regular.ttf

Dependency Overrides

Force a specific version when there are conflicts.

yaml
dependency_overrides:
  intl: 0.18.0  # Force this version

Package Installation

Install All Dependencies

bash
flutter pub get

Add New Package

bash
flutter pub add http
flutter pub add provider

Add Dev Dependency

bash
flutter pub add dev:flutter_lints

Remove Package

bash
flutter pub remove http

Upgrade Packages

bash
flutter pub upgrade        # Upgrade all
flutter pub upgrade http   # Upgrade specific package

Check Outdated Packages

bash
flutter pub outdated

Complete Example

yaml
name: ecommerce_app
description: A beautiful e-commerce Flutter application
publish_to: 'none'
version: 2.1.0+15

environment:
  sdk: '>=3.0.0 <4.0.0'

dependencies:
  flutter:
    sdk: flutter
  
  # UI
  cupertino_icons: ^1.0.2
  google_fonts: ^6.0.0
  
  # State Management
  provider: ^6.0.5
  get: ^4.6.5
  
  # Networking
  http: ^1.1.0
  dio: ^5.3.0
  
  # Local Storage
  shared_preferences: ^2.2.0
  hive: ^2.2.3
  
  # Utils
  intl: ^0.18.0
  uuid: ^4.0.0

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^2.0.0
  build_runner: ^2.4.0
  mockito: ^5.4.0

flutter:
  uses-material-design: true
  
  assets:
    - assets/images/
    - assets/icons/
    - assets/data/products.json
    - assets/translations/
  
  fonts:
    - family: Poppins
      fonts:
        - asset: fonts/Poppins-Regular.ttf
        - asset: fonts/Poppins-Medium.ttf
          weight: 500
        - asset: fonts/Poppins-Bold.ttf
          weight: 700

Best Practices

Important: Always run

text
flutter pub get
after modifying pubspec.yaml

✅ Do

yaml
# Good - Use version constraints
dependencies:
  http: ^1.1.0

# Good - Organize assets in folders
assets:
  - assets/images/
  - assets/icons/

❌ Don't

yaml
# Bad - Use 'any' version
dependencies:
  http: any

# Bad - List individual files when folder is better
assets:
  - assets/images/image1.png
  - assets/images/image2.png
  - assets/images/image3.png
  # ... 100 more files

Keep Dependencies Updated

bash
# Check for updates regularly
flutter pub outdated

# Upgrade safely
flutter pub upgrade

Use Exact Versions in Production

yaml
# Development - use constraints
dependencies:
  http: ^1.1.0

# Production - use exact versions
dependencies:
  http: 1.1.0

Common Errors

Error: "Pub get failed"

Solution: Delete

text
pubspec.lock
and run
text
flutter pub get

bash
rm pubspec.lock
flutter pub get

Error: "Asset not found"

Solution: Ensure path is correct and run:

bash
flutter clean
flutter pub get

Error: "Version conflict"

Solution: Use

text
dependency_overrides

yaml
dependency_overrides:
  intl: 0.18.0

YAML Syntax Rules

  • Use 2 spaces for indentation (not tabs)
  • Use lowercase for package names
  • Use underscores in names, not hyphens
  • Quote version constraints:
    text
    '>=3.0.0 <4.0.0'
  • Be careful with indentation (YAML is strict)

Resources