Dart - Project Components, Packages & Running a Dart Application

Photo by CHUTTERSNAP on Unsplash

Dart - Project Components, Packages & Running a Dart Application

Day 2 of my 100-day Flutter learning journey This blog will cover my understanding of what I've learned thus far. Let's get in.

DART PROJECT COMPONENTS

A Dart Project consists of various folders and files, which can be grouped into Dart Packages, Library Files, Git Source Control Files, Lint,Testing and Configuration files. But My focus will be more on Dart packages.

What are Dart Packages?

A Dart package is a cluster of code that other Dart projects can reuse for different purposes, i.e., a Dart package acts as a tool. Some of these packages or tools contain code to help you connect to specific APIs, add certain widgets to your app, or even help you write better code.

A good characteristic of Dart packages is their inter-dependability. Yes, other developers can depend on your packages and vice versa, so long as a developer publishes it to the Dart package repository (pub.dev). So once we put our tool(Dart packages) in our public toolbox (pub.dev), it can be accessed and used by the Dart developer community. Hence, a package not uploaded publicly will be called an application package, and a package uploaded publicly is called a library package.

Why are Dart packages essential? Well, they save developers the time of writing code for problems that have already been solved and are working well, making development efficient

Every Dart project has a Package Manager. Let us take a look at that next.

DART PACKAGE MANAGER

A Dart Package Manager helps you manage dependencies in your project. Dependencies are the packages on which your project relies. The Dart package manager performs this task using a configuration file pubspec.yaml , found in your Dart project folder. The pubspec.yaml file contains the name and SDK constraints of the project and a list of external packages your project is dependent on.

DART LIBRARIES

A Library is the only part of a Dart project publicly accessible to everyone, i.e. a Dart project can contain many libraries, whereas a library cannot have a project. So when importing a package into your project, you only have access to libraries located in the lib folder in a Dart project folder.

DART LINTING

Dart linting is the process Dart uses to analyze Dart code to find possible errors before you run your code. Linting is done using a set of predefined rules used by the Dart static analyzer during compilation. These rules are called lint rules.

RUNNING A DART APPLICATION

Dart executes a program with a tool part of the Dart SDK called the Dart Virtual Machine(VM). The Dart VM performs several steps to execute Dart code with the aid of components such as the Runtime system, JIT & AOT compilation pipelines, and others. The Dart VM can run Dart apps in two ways;

  1. Executing source code using the JIT/AOT compilers

  2. Executing source code using Snapshots(JIT, AOT or Kernel snapshots).

I will write on executing code using the JIT and AOT compilers.

RUNNING CODE FROM SOURCE USING JIT COMPILER

The Dart VM is incapable of executing raw Dart code, so for Dart VM to execute code using JIT, the source code has to be;

  1. Parsed the source code has to be read, and its structure analyzed and parsed into a small high-level intermediary Dart language called Abstract Syntax TreeAST

  2. Compilation The JIT compiler takes the AST and generates unoptimised machine code.

  3. Execution The Dart VM runs the unoptimised machine code producing the program output. While the program keeps running, the JIT compiler analyses the code as it executes, looking for opportunities to optimise the machine code further.

  4. Optimisation The JIT compiler identifies and replaces inefficient code that can be optimised with efficient machine code. The optimised code is then executed by the CPU, making the program run faster.

In a nutshell, the JIT compiler allows the Dart VM to run Dart code faster than it could with pure interpretation. By compiling and optimizing the code as it runs, the JIT compiler produces efficient machine code that can make programs run faster and more smoothly.

RUNNING CODE FROM SOURCE USING AOT COMPILER

When you use the Ahead-of-Time (AOT) compiler in Dart, the source code is compiled into machine code ahead of time rather than at runtime. Here is how it works;

  1. Parsing The Dart compiler reads the source code and analyzes its structure to create an abstract syntax tree (AST).

  2. Compilation The AOT compiler takes the AST and generates optimised machine code tailored to specific target platforms (such as x86 or ARM). This machine code is then stored in a binary file.

  3. Execution When you run the program, the machine code in the binary file is loaded directly into memory and executed by the CPU, producing the program's output.

Although using the AOT compiler is a step shorter than using the JIT compiler, using the AOT compiler can be more time-consuming and complex since you have to compile the code separately for each platform you want to target.