Welcome to the world of Dart! Before you can start building amazing applications with frameworks like Flutter or creating powerful command-line tools, you need to set up your development environment. This guide will walk you through the entire process, from installing the Dart SDK to compiling your first native executable. Let's get started!
Step 1: Installing the Dart SDK
The Dart SDK (Software Development Kit) is a collection of essential tools for developing Dart applications. It includes the compiler, runtime, package manager (pub), and core libraries. Here’s how to get it installed on your system.
1.1. Download and Install the Dart SDK
The recommended way to install the Dart SDK varies by operating system. You can download it directly or use a popular package manager.
For Windows (using Chocolatey)
If you use the Chocolatey package manager, you can install the SDK with a single command in your terminal:
choco install dart-sdk
For macOS (using Homebrew)
If you use the Homebrew package manager, run this command:
brew tap dart-lang/dart
brew install dart
Manual Installation (All Platforms)
You can always download the SDK as a ZIP file from the official Dart website. After downloading, extract the `dart-sdk` folder to a location of your choice. A common location is `C:\tools\dart-sdk` on Windows or `~/dev/dart-sdk` on macOS/Linux.
1.2. Set Up Your Environment Variables (PATH)
To run Dart commands like dart
and pub
from any terminal window, you must add the Dart SDK's `bin` directory to your system's PATH environment variable. This tells your system where to find the Dart executables.
For Windows:
- In the Start search bar, type "env" and select "Edit the system environment variables".
- Click the "Environment Variables..." button.
- In the "User variables" section, find the `Path` variable and click "Edit...".
- Click "New" and add the full path to the `bin` folder inside your extracted `dart-sdk` directory (e.g.,
C:\tools\dart-sdk\bin
). - Click "OK" on all windows to save your changes. You may need to restart your terminal for the changes to take effect.
For macOS & Linux:
Add the following line to your shell's configuration file (e.g., `~/.zshrc`, `~/.bash_profile`, or `~/.bashrc`). Remember to replace `[PATH_TO_DART_SDK]` with the actual path where you extracted the SDK.
export PATH="$PATH:[PATH_TO_DART_SDK]/bin"
Then, run source ~/.zshrc
(or your respective config file) or restart your terminal.
1.3. Verify the Installation
Open a new terminal and run the following command to confirm that Dart is installed correctly:
dart --version
If the installation was successful, you will see the installed Dart SDK version printed on the screen.
Step 2: Creating Your First Dart Project
With the SDK installed, you're ready to create a new Dart project. The `dart create` command generates a simple, well-structured application template to get you started.
2.1. Create a Project from a Template
Open your terminal, navigate to the directory where you want to create your project, and run the following command:
dart create my_first_app
This command creates a new directory named my_first_app
containing a simple command-line application. You can replace `my_first_app` with any project name you like.
2.2. Understanding the Dart Project Structure
The generated project has a standard layout. Here’s a look at the key files and directories:
my_first_app/
├── .dart_tool/
├── .gitignore
├── analysis_options.yaml
├── CHANGELOG.md
├── lib/
│ └── my_first_app.dart
├── bin/
│ └── my_first_app.dart
├── pubspec.yaml
├── pubspec.lock
└── README.md
pubspec.yaml
: The most important file. It defines project metadata (name, description, version) and manages dependencies on other packages.bin/
: Contains the main entry point for your executable application. Code here is designed to be run directly.lib/
: Contains the private and public library code for your project. Most of your application logic will live here.pubspec.lock
: An auto-generated file that locks down the exact versions of all your project's dependencies. You should not edit this file manually.analysis_options.yaml
: A configuration file for the Dart analyzer. You can define custom linting rules here to enforce code style and catch potential errors..dart_tool/
: A hidden directory where Dart tools store generated files, including the `package_config.json` file which maps package names to their locations on disk.
Step 3: Running Your Dart Application
Before compiling, you can run your Dart code directly using the Dart VM (Virtual Machine). This uses a Just-In-Time (JIT) compiler, which is great for fast development cycles.
3.1. The "Hello World" Code
The file bin/my_first_app.dart
created by the template contains a simple "Hello World" program. It will look something like this:
import 'package:my_first_app/my_first_app.dart' as my_first_app;
void main(List arguments) {
print('Hello world: ${my_first_app.calculate()}!');
}
3.2. Execute the Script
To run your application, navigate into your project directory (cd my_first_app
) and use the dart run
command:
dart run
This command finds the main entry point in the `bin/` directory and executes it. You should see the output `Hello world: 42!` in your terminal.
Step 4: Compiling to a Native Executable
When you're ready to distribute your application, you can compile it into a standalone native executable. This process, known as Ahead-Of-Time (AOT) compilation, creates a fast, self-contained file that can run on other machines without the Dart SDK installed.
4.1. Compile the Dart File
To compile your application, use the dart compile
command with the `exe` subcommand. Make sure you are in your project's root directory.
dart compile exe bin/my_first_app.dart
This command will compile your code and create an executable file inside the `bin/` directory. The output file will be named `my_first_app.exe` on Windows and `my_first_app` on macOS and Linux.
4.2. Run the Compiled Executable
You can now run this compiled file directly from your terminal. The command differs slightly between operating systems.
On Windows:
.\bin\my_first_app.exe
On macOS or Linux:
./bin/my_first_app
Running this command will execute your compiled application, and you'll see the same "Hello world" output as before. Congratulations, you've successfully installed, created, ran, and compiled a Dart application!
0 개의 댓글:
Post a Comment