Dart:
Starting a new project

How to:

  1. Install Dart: Ensure Dart is installed on your system. If not, you can download it from https://dart.dev/get-dart. Verify the installation with:

    dart --version
  2. Create a New Dart Project: Use the Dart CLI to generate a new project:

    dart create hello_dart

    This command creates a new directory hello_dart with a simple sample web or console application, depending on your selection.

  3. Examine the Project Structure:

    Navigate to your project directory:

    cd hello_dart

    A typical Dart project includes the following key files and directories:

    • pubspec.yaml: Configuration file that includes your project’s dependencies and SDK constraints.
    • lib/: Directory where most of the Dart code resides.
    • test/: Directory for project tests.
  4. Add Dependencies: Edit pubspec.yaml to add dependencies. For web projects, consider adding http, a popular package for making HTTP requests:

    dependencies:
      flutter:
        sdk: flutter
      http: ^0.13.3

    After editing, get the dependencies:

    dart pub get
  5. Write Your First Dart Code:

    In the lib/ directory, create a new Dart file, main.dart, and add a simple Dart code:

    // Import the Dart core library
    import 'dart:core';
    
    void main() {
      print('Hello, Dart!');
    }
  6. Run Your Dart Application:

    Execute your Dart program with:

    dart run

    The output should be:

    Hello, Dart!

By following these steps, you’ve successfully started a new Dart project, from installation to running your first piece of Dart code. This foundational knowledge sets the stage for diving deeper into Dart’s rich ecosystem and its capabilities for building scalable applications.