Flutter Setup & First App

Flutter CLI Commands

40 min Lesson 11 of 12

The Flutter Command-Line Interface

The Flutter CLI is your primary tool for creating, running, building, and managing Flutter projects from the terminal. Mastering these commands will make you significantly more productive. Every Flutter developer should be comfortable working with the CLI, even if they primarily use an IDE.

Note: All Flutter CLI commands are prefixed with flutter. You can always run flutter --help to see a complete list of available commands, or flutter <command> --help for detailed help on a specific command.

flutter create

The flutter create command generates a new Flutter project with all the necessary files and directory structure.

Basic Project Creation

# Create a new Flutter project
flutter create my_app

# Create with a custom organization (reverse domain)
flutter create --org com.example my_app

# The --org flag sets the bundle ID:
# Android: com.example.my_app
# iOS: com.example.myApp

Platform Selection

By default, Flutter creates a project for all supported platforms. You can limit this with the --platforms flag:

Platform-Specific Creation

# Create for specific platforms only
flutter create --platforms=android,ios my_mobile_app
flutter create --platforms=web my_web_app
flutter create --platforms=windows,macos,linux my_desktop_app

# Create for all platforms (default behavior)
flutter create --platforms=android,ios,web,windows,macos,linux my_app

Project Templates

The -t (or --template) flag lets you choose different project types:

Project Templates

# Standard Flutter app (default)
flutter create -t app my_app

# Flutter package (reusable library)
flutter create -t package my_package

# Flutter plugin (platform-specific code)
flutter create -t plugin my_plugin

# Flutter plugin with FFI (Foreign Function Interface)
flutter create -t plugin_ffi my_ffi_plugin

# Skeleton app (opinionated starter with best practices)
flutter create -t skeleton my_app
Tip: The skeleton template creates a project with a more advanced structure including localization support, theme configuration, and a settings screen. It’s a great starting point for production apps.

flutter run

The flutter run command builds and launches your app on a connected device or emulator.

Running Your App

# Run on the default device
flutter run

# Run on a specific device (use flutter devices to find ID)
flutter run -d chrome
flutter run -d emulator-5554
flutter run -d 'iPhone 15 Pro'

# Run in different modes
flutter run --debug      # Debug mode (default) - hot reload, assertions
flutter run --profile    # Profile mode - performance profiling
flutter run --release    # Release mode - optimized, no debugging

# Run on all connected devices simultaneously
flutter run -d all

Build Modes Explained

Flutter has three build modes, each serving a different purpose:

  • Debug mode (--debug): Hot reload enabled, assertions active, unoptimized. Best for development.
  • Profile mode (--profile): Some optimizations, performance overlay available. Best for finding performance issues.
  • Release mode (--release): Fully optimized, no debugging tools. Best for final testing and distribution.
Warning: Profile mode is not available on emulators/simulators. You must use a physical device for profiling because emulators don’t accurately represent real device performance.

Hot Reload and Hot Restart

While your app is running in debug mode, you can use these keyboard shortcuts in the terminal:

Runtime Keyboard Shortcuts

# While flutter run is active in the terminal:
r    # Hot reload - applies code changes instantly
R    # Hot restart - restarts the app, resets state
p    # Toggle debug paint overlay
o    # Toggle between Android/iOS platform rendering
q    # Quit the app
h    # Show all available commands
d    # Detach (keep app running, stop CLI)

flutter build

The flutter build command compiles your app into a distributable format for the target platform.

Building for Different Platforms

# Android APK (universal)
flutter build apk

# Android APK split by ABI (smaller downloads)
flutter build apk --split-per-abi

# Android App Bundle (recommended for Play Store)
flutter build appbundle

# iOS (requires macOS and Xcode)
flutter build ios

# iOS without codesigning (for CI/CD)
flutter build ios --no-codesign

# Web
flutter build web

# Web with a custom base path
flutter build web --base-href /my-app/

# Windows desktop
flutter build windows

# macOS desktop
flutter build macos

# Linux desktop
flutter build linux
Note: For the Google Play Store, always use flutter build appbundle instead of flutter build apk. App Bundles let Google Play generate optimized APKs for each device configuration, resulting in smaller downloads.

Build Output Locations

# APK output:
build/app/outputs/flutter-apk/app-release.apk

# App Bundle output:
build/app/outputs/bundle/release/app-release.aab

# Web output:
build/web/

# iOS output:
build/ios/iphoneos/Runner.app

# Windows output:
build/windows/x64/runner/Release/

# macOS output:
build/macos/Build/Products/Release/

flutter doctor

The flutter doctor command checks your development environment and reports any issues.

Environment Check

# Quick check
flutter doctor

# Verbose check with detailed information
flutter doctor -v

# Example output:
# Doctor summary (to see all details, run flutter doctor -v):
# [✓] Flutter (Channel stable, 3.19.0)
# [✓] Android toolchain - develop for Android devices
# [✓] Xcode - develop for iOS and macOS
# [✓] Chrome - develop for the web
# [✓] Android Studio (version 2023.1)
# [✓] VS Code (version 1.85.0)
# [✓] Connected device (2 available)
# [✓] Network resources
#
# • No issues found!
Tip: Run flutter doctor -v whenever you encounter build errors. It often reveals missing SDKs, license agreements that need acceptance, or configuration issues that cause problems.

flutter clean

The flutter clean command removes the build/ directory and other generated files. Use it when you encounter mysterious build errors.

Cleaning the Project

# Remove build artifacts
flutter clean

# After cleaning, you need to get dependencies again
flutter clean && flutter pub get

# Full clean rebuild workflow
flutter clean
flutter pub get
flutter run
Note: Always run flutter pub get after flutter clean because cleaning removes the .dart_tool/ directory which contains dependency resolution data.

flutter pub Commands

The flutter pub family of commands manages your project’s dependencies:

Dependency Management Commands

# Install/resolve dependencies
flutter pub get

# Upgrade to latest allowed versions
flutter pub upgrade

# Upgrade a specific package
flutter pub upgrade http

# Force major version upgrades
flutter pub upgrade --major-versions

# Check for outdated dependencies
flutter pub outdated

# View dependency resolution
flutter pub deps

# Clear the global pub cache
flutter pub cache clean

# Repair the global pub cache
flutter pub cache repair

# Add a package
flutter pub add provider

# Add a dev dependency
flutter pub add --dev build_runner

# Remove a package
flutter pub remove provider

flutter analyze

The flutter analyze command runs static analysis on your code to find potential errors, style issues, and best practice violations.

Code Analysis

# Analyze the entire project
flutter analyze

# Example output:
# Analyzing my_app...
#
#   info - Unused import - lib/models/old_model.dart:1:8
#   warning - The parameter 'key' is deprecated - lib/screens/home.dart:15:3
#   error - The argument type 'String' can't be assigned to 'int' - lib/utils.dart:23:10
#
# 1 error, 1 warning, 1 info found.

flutter test

The flutter test command runs your project’s tests:

Running Tests

# Run all tests
flutter test

# Run a specific test file
flutter test test/widget_test.dart

# Run tests with coverage
flutter test --coverage

# Run tests matching a name pattern
flutter test --name 'counter increments'

# Run tests with verbose output
flutter test --reporter expanded

flutter devices

The flutter devices command lists all connected devices and emulators available for running your app:

Listing Devices

# List all connected devices
flutter devices

# Example output:
# 3 connected devices:
#
# Pixel 7 (mobile)   • emulator-5554 • android-arm64  • Android 14 (API 34)
# iPhone 15 (mobile) • ABC12345-...  • ios             • iOS 17.2 (simulator)
# Chrome (web)       • chrome        • web-javascript  • Google Chrome 120
# macOS (desktop)    • macos         • darwin-arm64    • macOS 14.2

# List available emulators
flutter emulators

# Launch an emulator
flutter emulators --launch Pixel_7_API_34

flutter config

The flutter config command manages Flutter SDK settings:

Configuration Commands

# View current config
flutter config

# Enable/disable platform support
flutter config --enable-web
flutter config --enable-windows-desktop
flutter config --enable-macos-desktop
flutter config --enable-linux-desktop

# Disable a platform
flutter config --no-enable-web

# Set Android SDK path
flutter config --android-sdk /path/to/android/sdk

# Clear all settings
flutter config --clear-features

flutter channel

Flutter has multiple release channels. The flutter channel command lets you switch between them:

Channel Management

# View current channel
flutter channel

# Switch to a different channel
flutter channel stable   # Most stable, recommended for production
flutter channel beta     # Preview of upcoming features
flutter channel master   # Latest development, may be unstable

# After switching channels, upgrade to get the latest
flutter channel stable
flutter upgrade
Warning: Only use the stable channel for production apps. The beta and master channels may contain bugs or breaking changes that could affect your app’s reliability.

Additional Useful Commands

More CLI Commands

# Upgrade Flutter SDK to the latest version
flutter upgrade

# Downgrade Flutter SDK to the previous version
flutter downgrade

# Show Flutter SDK version information
flutter --version

# Show detailed SDK path info
flutter sdk-path

# Create a screenshot of a running app
flutter screenshot

# Attach to a running Flutter app
flutter attach -d <device-id>

# Generate localizations
flutter gen-l10n

# View logs from a running device
flutter logs

Command Cheat Sheet

Here is a quick reference for the most frequently used commands:

Quick Reference

# Project Setup
flutter create --org com.mycompany my_app
flutter pub get

# Daily Development
flutter run -d chrome
flutter analyze
flutter test

# Building for Release
flutter build apk --split-per-abi
flutter build appbundle
flutter build ios
flutter build web

# Troubleshooting
flutter doctor -v
flutter clean && flutter pub get
flutter pub cache repair

# Package Management
flutter pub add <package>
flutter pub upgrade --major-versions
flutter pub outdated

Summary

In this lesson, you mastered the Flutter CLI commands that every developer needs:

  • flutter create with organization, platform, and template options
  • flutter run with device selection and build modes
  • flutter build for APK, App Bundle, iOS, and web targets
  • flutter doctor -v for environment diagnostics
  • flutter clean to resolve mysterious build errors
  • flutter pub family for dependency management
  • flutter analyze and flutter test for code quality
  • flutter devices, flutter config, and flutter channel for setup
Tip: Create shell aliases for commands you use frequently. For example: alias fr="flutter run", alias fpg="flutter pub get", alias fb="flutter build apk --split-per-abi". This saves significant time over the course of a project.