On this page
Hello World in Dart
Creating a Dart Project Using Visual Studio Code with Dart Extension
Step 1: Open Visual Studio Code
Launch Visual Studio Code on your computer.
Step 2: Install Dart Extension (if not installed)
- Go to the Extensions view by clicking the Extensions icon in the Activity Bar on the side of the window or by pressing
Ctrl+Shift+X
. - Search for “Dart” and install the Dart extension provided by Dart Code.
Step 3: Create a New Dart Project
- Open the Command Palette by pressing
Shift+Command+P
(macOS) orShift+Ctrl+P
(Windows/Linux). - Type
Dart: New Project
and select it from the list. - Visual Studio Code will prompt you to choose a location for your new Dart project:
- Click on “Open Folder” to open a file picker dialog.
- Navigate to the desired directory where you want to create your Dart project.
- Create a new folder if necessary and enter a name for your Dart project.
- Click “Open” or “Select Folder” to confirm.
Step 4: Choose a Project Template
-
After confirming the project location and name, you will see a list of available project templates:
- Console Application: A basic Dart console application.
- Package: A Dart package project.
- Web Application: A Dart web application using
webdev
tool. - Flutter Application: A new Flutter project.
- Server Application: A Dart server-side application.
-
Select the template that best suits your project needs by clicking on it or using the arrow keys and pressing
Enter
.
Step 5: Find the Location of Your Dart Project
- Visual Studio Code will create the project structure at the specified location.
Step 6: Enter a name as root folder of the project
Step 7: Run Your Dart Program (Optional)
- If you created a default Dart application as part of the new project, you can run it by navigating to the project directory in the integrated terminal and using the command:
dart bin/main.dart
Creating a Dart Project Using dart create
-
Open Terminal:
- Open your terminal or command prompt.
-
Navigate to Desired Directory:
- Use
cd
command to navigate to the directory where you want to create your Dart project.
- Use
cd path/to/desired-directory
-
Create Dart Project:
- Run the
dart create
command followed by your desired project name (replaceproject-name
with your chosen project name).
- Run the
dart create project-name
-
Navigate into Project Directory:
- After the project is created, navigate into the newly created project directory.
cd project-name
-
Explore Your Dart Project:
- Once inside the project directory, you’ll find the default project structure generated by Dart’s create command, including lib, bin, and test directories, along with configuration files like pubspec.yaml.
Dart Project Structure Overview
Your Dart project follows a standard structure with the following directories and files:
.dart_tool
: Contains tools and configurations specific to Dart projects.bin/
: Contains executable Dart scripts that serve as entry points for your application.lib/
: Contains Dart source code files that define libraries and reusable components for your application.test/
: Contains unit tests and integration tests for testing your Dart code..gitignore
: Specifies files and directories that Git should ignore.analysis_options.yaml
: Configuration file for Dart’s static analysis tool (dart analyze).changelog.md
: Records significant changes made to the Dart project over time.pubspec.lock
: Lock file that specifies exact versions of dependencies used in the project.pubspec.yaml
: Configuration file that defines the Dart project’s metadata, dependencies, and other settings.readme.md
: Markdown file providing documentation and information about the Dart project.
Code Explanation
lib/dart_play.dart
// lib/dart_play.dart
int calculate() {
return 6 * 7;
}
- Function Definition: Defines a Dart function
calculate()
inlib/dart_play.dart
. - Functionality: Computes the product of 6 and 7, returning the result (
42
).
bin/dart_play.dart
// bin/dart_play.dart
import 'package:dart_play/dart_play.dart' as dart_play;
void main(List<String> arguments) {
print('Hello world: ${dart_play.calculate()}!');
}
- Import Statement: Imports the
calculate()
function fromlib/dart_play.dart
using thedart_play
package alias. - Main Function: Entry point (
main()
) of the Dart application inbin/dart_play.dart
. - Function Invocation: Calls
dart_play.calculate()
to compute and print42
within the console output.
Summary
- Project Organization: Standard Dart project structure facilitates code organization and maintainability.
- Code Functionality: Demonstrates modular code development with reusable functions (
lib/dart_play.dart
) and executable scripts (bin/dart_play.dart
). - Usage: Illustrates how Dart applications are structured and executed, emphasizing separation of logic and entry points for effective development practices.