What is Dart
Dart is a client-optimized programming language for fast multi-platform apps.
It is a modern, UI-focused language that’s compiled to native ARM or x86 code or cross-compiled(transpiled) to Javascript.
The Dart virtual machine allows lightning fast development-time rebuilds, its JavaScript complier allows you to build for the web, and its ahead-of-time compiler creates fast native applications across mobile and desktop platforms and even for servers and embedded devices.
It can also be used as an interpreted language because of Dart VM.
Dart’s compiler technology lets you run code in different ways:
Native Platform
- For apps targeting mobile and desktop devices, Dart includes both a Dart VM with just-in-time (JIT) compilation and an ahead-of-time (AOT) compiler for producing machine code.
- During development, a fast developer cycle is critical for iteration. The Dart VM offers a just-in-time compiler (JIT) with incremental recompilation (enabling hot reload), live metrics collections (powering DevTools), and rich debugging support.
- When apps are ready to be deployed to production — the Dart AOT compiler enables ahead-of-time compilation to native ARM or x64 machine code. The AOT-compiled app launches with consistent, short startup time.
- The AOT-compiled code runs inside an efficient Dart runtime that enforces the sound Dart type system and manages memory using fast object allocation and a generational garbage collector.
Web Platform
- For apps targeting the web, Dart includes both a development time compiler (dartdevc) and a production time compiler (dart2js). Both compilers translate Dart into JavaScript.
- Dart Web enables running Dart code on web platforms powered by JavaScript. With Dart Web, you compile Dart code to JavaScript code, which in turn runs in a browser — for example, V8 inside Chrome.
- Dart web contains both an incremental dev compiler enabling a fast developer cycle, and an optimizing production compiler, dart2js, which compiles Dart code to fast, compact, deployable JavaScript using techniques such as dead-code elimination.
Regardless of which platform you use or how you compile your code, executing the code requires a Dart runtime which is responsible for the following critical tasks:
- Managing memory: Dart uses a managed memory model, where unused memory is reclaimed by a garbage collector (GC).
- Enforcing the Dart type system: Although most type checks in Dart are static (compile-time), some type checks are dynamic (runtime). For example, the Dart runtime enforces dynamic checks by type check and cast operators.
- Managing isolates: The Dart runtime controls the main isolate (where code normally runs) and any other isolates that the app creates.
- Isolate is just a chunk of memory running a single threaded event loop. This makes it possible to perform async background work on a single thread.
- One can also spawn multiple isolates to run code in parallel.
- Think of it like a Worker Thread in JS.
On native platforms, the Dart runtime is automatically included inside self-contained executables, and is part of the Dart VM provided by the dart run command.
Dart is designed for a technical envelope that is particularly suited to client development, prioritizing both development (sub-second stateful hot reload) and high-quality production experiences across a wide variety of compilation targets (web, mobile, and desktop).
Dart CLI
Library Support
Dart has a rich set of **core libraries(**built in), providing essentials for many everyday programming tasks.
- dart:core provides ****Built-in types, collections, and other core functionality for every Dart program.
- dart:collection provides Richer collection types such as queues, linked lists, hashmaps, and binary trees.
- dart:convert provides Encoders and decoders for converting between different data representations, including JSON and UTF-8.
- dart:math provides Mathematical constants and functions, and random number generation.
- dart:io provides File, socket, HTTP, and other I/O support for non-web applications.
- dart:async provides Support for asynchronous programming, with classes such as Future and Stream.
- dart:typed_data provides Lists that efficiently handle fixed-sized data (for example, unsigned 8-byte integers) and SIMD numeric types.
- dart:ffi provides Foreign function interfaces for interoperability with other code that presents a C-style interface.
- dart:isolate provides Concurrent programming using isolates — independent workers that are similar to threads but don’t share memory, communicating only through messages.
- dart:html provides HTML elements and other resources for web-based applications that need to interact with the browser and the Document Object Model (DOM).
The Dart team publishes many useful supplementary packages, that one can get from pub (dart package manager).
- characters
- intl
- http
- crypto
- markdown
Third-party publishers and the broader community publish thousands of packages, that one can get from pub. (By Community 💖)
- XML
- Windows integration
- SQLite
- compression
Important Concepts
Everything you can place in a variable is an object, and every object is an instance of a Object class.
- Numbers, functions, null everything are objects.
- With the exception of null (if you enable sound null safety), all objects inherit from the Object class.
In Dart statements and expressions ends with a semicolon (;). Semicolons give the compiler the context it needs to properly understand the code.
Although Dart is strongly typed, type annotations are optional because Dart can infer types.
If you enable null safety, variables can’t contain null unless you say they can. To make a variable nullable put a question mark (?) at the end of its type.
-
If you know that an expression never evaluates to null but Dart disagrees, you can add ! to assert that it isn’t null (and to throw an exception if it is).
int x = nullableButNotNullInt!
When you want to explicitly say that any type is allowed, use the type Object?, Object, or dynamic, if you must defer type checking until runtime (Runtime Type Checking or Type Inference).
Dart supports top-level functions (such as main()), as well as functions tied to a class or object (static and instance methods, respectively). You can also create functions within functions (nested or local functions).
Dart supports top-level variables, as well as variables tied to a class or object (static and instance variables). Instance variables are sometimes known as fields or properties.
Unlike Java, Dart doesn’t have the keywords public, protected, and private (access specifiers). If an identifier starts with an underscore (_), it’s private to its library.
- Prefixing an identifier with an underscore (_) makes the identifier private to its library.
Identifiers can start with a letter or underscore (_), followed by any combination of those characters plus digits.
Dart has both expressions (which have runtime values) and statements.
- A statement is a command, something you tell the computer to do.
- An expression doesn’t do something; it is something i.e. a value, or is something that can be calculated as a value.
- A statement often contains one or more expressions, but an expression can’t directly contain a statement.