Dart programming language

Unnamed File 33 - Dart Programming Language - Android

Optimized for UI

  • A programming language optimized for building user interfaces with features such as the spread operator for expanding collections, and collection if for customizing UI for each platform
  • A programming language that is easy to learn, with a familiar syntax

Productive Development

  • Make changes to your source code iteratively, using hot reload to instantly see the effect in the running app
  • Write code using a flexible type system with rich static analysis and powerful, configurable tooling
  • Do profiling, logging, and debugging with your code editor of choice

Fast on all platforms

  • AOT-compile apps to native machine code for instant startup
  • Run backend code supporting your app, written using a single programming language

Optimized for UI

  • A programming language optimized for building user interfaces with features such as the spread operator for expanding collections, and collection if for customizing UI for each platform
  • A programming language that is easy to learn, with a familiar syntax

Productive development

  • Make changes to your source code iteratively, using hot reload to instantly see the effect in the running app
  • Write code using a flexible type system with rich static analysis and powerful, configurable tooling
  • Do profiling, logging, and debugging with your code editor of choice

Fast on all platforms

  • AOT-compile apps to native machine code for instant startup
  • Run backend code supporting your app, written using a single programming language

Want more practice? Try a codelab. Or download Dart.

Hello, World!

main() { print(‘Hello, World!’); }

Control flow

bool isEven(int x) { // An if-else statement. if (x % 2 == 0) { return true; } else { return false; } } List getEvenNumbers(Iterable numbers) { var evenNumbers = []; // A for-in loop. for (var i in numbers) { // A single-line if statement. if (isEven(i)) evenNumbers.add(i); } return evenNumbers; } main() { var numbers = List.generate(10, (i) => i); print(getEvenNumbers(numbers)); }

Strings

main() { print(‘a single quoted string’); print(“a double quoted string”); // Strings can be combined with the + operator. print(‘cat’ + ‘dog’); // Triple quotes define a multi-line string. print(”’triple quoted strings are for multiple lines”’); // Dart supports string interpolation. const pi = 3.14; print(‘pi is $pi’); print(‘tau is ${2 * pi}’); }

Collection literals

// A list literal. var lostNumbers = [4, 8, 15, 16, 23, 42]; // A map literal. var nobleGases = { ‘He’: ‘Helium’, ‘Ne’: ‘Neon’, ‘Ar’: ‘Argon’, }; // A set literal. var frogs = { ‘Tree’, ‘Poison dart’, ‘Glass’, }; main() { print(lostNumbers.last); print(nobleGases[‘Ne’]); print(frogs.difference({‘Poison dart’})); }

Classes

// Abstract classes can’t be instantiated. abstract class Item { use(); } // Classes can implement other classes. class Chest implements Item { List contents; // Constructors can assign arguments to instance variables using `this`. Chest(this.contents); use() => print(“$this has ${contents.length} items.”); } class Sword implements Item { int damage = 5; use() => print(“$this dealt $damage damage.”); } // Classes can extend other classes. class DiamondSword extends Sword { int damage = 50; } main() { // The ‘new’ keyword is optional. var chest = Chest([ DiamondSword(), Sword(), ]); chest.use(); for (var item in chest.contents) { item.use(); } }

Compute π

import ‘dart:math’ show Random; main() async { print(‘Compute π using the Monte Carlo method.’); await for (var estimate in computePi().take(100)) { print(‘π ≅ $estimate’); } } /// Generates a stream of increasingly accurate estimates of π. Stream computePi({int batch = 100000}) async* { var total = 0; var count = 0; while (true) { var points = generateRandom().take(batch); var inside = points.where((p) => p.isInsideUnitCircle); total += batch; count += inside.length; var ratio = count / total; // Area of a circle is A = π⋅r², therefore π = A/r². // So, when given random points with x ∈ <0,1>, // y ∈ <0,1>, the ratio of those inside a unit circle // should approach π / 4. Therefore, the value of π // should be: yield ratio * 4; } } Iterable generateRandom([int seed]) sync* { final random = Random(seed); while (true) { yield Point(random.nextDouble(), random.nextDouble()); } } class Point { final double x, y; const Point(this.x, this.y); bool get isInsideUnitCircle => x * x + y * y <= 1; }
Source

Leave a Reply