Dart
In short
Dart is a statically typed, garbage-collected language from Google that compiles to native code and JavaScript, best known as the language of Flutter apps.
What is Dart?
Dart is a general-purpose, object-oriented programming language developed by Google and first released in 2011. It was originally pitched as an alternative to JavaScript in the browser, but today it is best known as the language behind Flutter, an open-source toolkit for building mobile, web, desktop and embedded apps from a single codebase. Dart 3, released in 2023, made sound null safety mandatory and added records and pattern matching.
Dart is statically typed with type inference, and its null safety is sound, meaning a variable of type String can never hold null; only String? can. The language has two compilation modes. During development, a just-in-time compiler enables hot reload, which updates a running app almost instantly without losing its state, and for release builds an ahead-of-time compiler produces fast native machine code, or JavaScript and WebAssembly for the web. Asynchronous code uses Future, Stream and async/await, and concurrency uses isolates, which work like threads that don't share memory.
Dart is used mainly for cross-platform user interfaces with Flutter, along with command-line tools and some server-side code. Its C-style syntax, classes and curly braces feel familiar to anyone who knows Java, C# or JavaScript, so it is usually quick to pick up.
Dart is often compared with TypeScript, since both add static types to a JavaScript-like style of programming. TypeScript is a typed layer on top of JavaScript and always compiles to JavaScript, while Dart is an independent language with its own runtime and libraries that can compile to native code as well as to JavaScript. Dart's types are also enforced while the program runs, whereas TypeScript's types are erased after compilation.
Key takeaways
- Dart is an object-oriented, statically typed language with sound null safety.
- It compiles ahead of time to native code, and to JavaScript or WebAssembly for the web.
- JIT compilation during development enables fast hot reload.
- It is best known as the language of the Flutter UI toolkit.
- Isolates provide concurrency without shared memory.
Example
class User {
final String name;
final String? email; // the ? means this may be null
User(this.name, [this.email]);
}
Future<void> main() async {
final users = [User('Ada', 'ada@example.com'), User('Grace')];
for (final user in users) {
await Future.delayed(const Duration(milliseconds: 100));
print('${user.name}: ${user.email ?? "no email"}');
}
}Readers ask
Is Dart only used for Flutter?
Flutter is by far its biggest use, but Dart also runs command-line tools, server applications and web apps compiled to JavaScript or WebAssembly.
Is Dart similar to JavaScript?
The syntax looks familiar to JavaScript developers, but Dart is statically typed with sound null safety and has its own runtime and standard library. It can compile to JavaScript, but it is a separate language.
Is Dart compiled or interpreted?
Dart is compiled in both stages. It uses a just-in-time compiler during development to support hot reload, and ahead-of-time compilation to native machine code or JavaScript for production builds.
See also
- TypeScriptWeb Development, p. 50TypeScript is a programming language built on JavaScript that adds static types, catching many bugs before the code runs, and compiles to plain JavaScript.
- JavaScriptWeb Development, p. 24JavaScript is the programming language that runs in web browsers to make pages interactive, and it also runs on servers through runtimes like Node.js.
- KotlinProgramming Languages, p. 18Kotlin is a concise, statically typed language that runs on the JVM, works seamlessly with Java, and is widely used for Android and server-side development.
- SwiftProgramming Languages, p. 31Swift is a compiled, statically typed language designed for safety and speed, used mainly to build apps for iPhone, iPad, Mac, and other Apple platforms.
- Garbage CollectionProgramming Fundamentals, p. 23Garbage collection is automatic memory management in which the language runtime finds data a program can no longer use and frees that memory for reuse.
- Async/AwaitProgramming Fundamentals, p. 4Async/await is a syntax for writing asynchronous code that reads like ordinary step-by-step code, pausing at each await until a promise or task finishes.
Spotted a mistake or something missing on this page?Suggest an edit