温馨提示×

Flutter与Ubuntu系统集成方法探讨

小樊
46
2025-10-28 14:26:01
栏目: 智能运维

Installing Flutter on Ubuntu
To integrate Flutter with Ubuntu, the first step is installing Flutter SDK. You can choose between three methods: using Snap (official and simplified), downloading the tarball (flexible for custom paths), or cloning the Git repository (suitable for developers who prefer source control).

  • Snap Method: Run sudo snap install flutter --classic after updating your system with sudo apt update && sudo apt upgrade. This installs Flutter with all dependencies managed by Snap.
  • Tarball Method: Download the latest stable release from the Flutter website (e.g., flutter_linux_xxx_stable.tar.xz), extract it to a directory (e.g., ~/flutter), and add the bin folder to your PATH by editing ~/.bashrc (or ~/.zshrc) with export PATH="$PATH:~/flutter/bin".
  • Git Method: Clone the stable channel repository using git clone -b stable https://github.com/flutter/flutter.git and follow the same environment variable steps as the tarball method.

After installation, run flutter doctor to verify setup and address missing dependencies (e.g., Android tools, GTK libraries).

Configuring Environment Variables
For Flutter to work globally, add its bin directory to your shell’s PATH. Open ~/.bashrc (Bash) or ~/.zshrc (Zsh) in a text editor (e.g., nano ~/.bashrc), append export PATH="$PATH:~/flutter/bin" (replace with your Flutter path if different), save the file, and run source ~/.bashrc (or source ~/.zshrc) to apply changes. This ensures Flutter commands (e.g., flutter run, flutter build) are accessible from any terminal.

Installing Dependencies
Flutter requires system tools and libraries for development. Install essential dependencies using:
sudo apt install git curl unzip wget zsh libgtk-3-dev clang cmake ninja-build
These tools support code formatting, asset management, and Linux desktop compilation. For Android development (optional but common), install Android Studio and the Flutter/Dart plugins via Android Studio’s plugin marketplace. Configure the Android SDK by running flutter doctor --android-licenses and accepting all licenses.

Verifying the Installation
Run flutter doctor to check the setup. The command outputs a summary of installed components (e.g., Flutter SDK, Android toolchain, Chrome for web) and highlights missing dependencies. Address any issues (e.g., “Linux toolchain” errors) by following the prompts (e.g., installing clang, cmake). A successful verification shows “[✓] All SDK package licenses accepted” and no red flags.

Creating and Running a Flutter Project
Once Flutter is set up, create a new project with flutter create my_flutter_app (replace my_flutter_app with your desired name). Navigate to the project directory (cd my_flutter_app) and run flutter run to launch the app. By default, Flutter uses an emulator (if configured) or a connected physical device. For Linux desktop development, ensure the “Linux toolchain” is installed (via flutter doctor) and run flutter run -d linux to target the desktop environment.

Optional: Enhancing the Development Experience

  • Hot Reload: Use flutter run with the --hot flag to enable hot reloading, which updates the UI without restarting the app.
  • DevTools: Install Flutter DevTools for debugging and performance analysis by running flutter pub global activate devtools and launching it with flutter pub global run devtools. Access the tool in your browser at http://localhost:4040.
  • Mirrors: For faster package downloads in China, configure Flutter mirrors by adding these lines to ~/.zshrc (or ~/.bashrc):
    export PUB_HOSTED_URL=https://mirrors.tuna.tsinghua.edu.cn/dart-pub
    export FLUTTER_STORAGE_BASE_URL=https://mirrors.tuna.tsinghua.edu.cn/flutter
    Then run source ~/.zshrc to apply changes.

0