// coding:utf-8 void main() { print('Hello, World!'); } var name = 'Voyager I'; var url = 'url' var year = 1977; var antennaDiameter = 3.7; var flybyObjects = ['Jupiter', 'Saturn', 'Uranus', 'Neptune']; var image = { 'tags': ['saturn'], url: '//path/to/saturn.jpg' }; if (year >= 2001) { print('21st century'); } else if (year >= 1901) { print('20th century'); } for (final object in flybyObjects) { print(object); } for (int month = 1; month <= 12; month++) { print(month); } while (year < 2016) { year += 1; } flybyObjects.where((name) => name.contains('turn')).forEach(print); // This is a normal, one-line comment. /// This is a documentation comment, used to document libraries, /// classes, and their members. Tools like IDEs and dartdoc treat /// doc comments specially. /* Comments like these are also supported. */ /** Comment block doc */ // Importing core libraries import 'dart:math'; // Importing libraries from external packages import 'package:test/test.dart'; // Importing files import 'path/to/my_other_file.dart'; class Spacecraft { String name; DateTime? launchDate; // Read-only non-final property int? get launchYear => launchDate?.year; // Constructor, with syntactic sugar for assignment to members. Spacecraft(this.name, this.launchDate) { // Initialization code goes here. } // Named constructor that forwards to the default one. Spacecraft.unlaunched(String name) : this(name, null); // Method. void describe() { print('Spacecraft: $name'); // Type promotion doesn't work on getters. var launchDate = this.launchDate; if (launchDate != null) { int years = DateTime.now().difference(launchDate).inDays ~/ 365; print('Launched: $launchYear ($years years ago)'); } else { print('Unlaunched'); } } } var voyager = Spacecraft('Voyager I', DateTime(1977, 9, 5)); voyager.describe(); var voyager3 = Spacecraft.unlaunched('Voyager III'); voyager3.describe(); enum PlanetType { terrestrial, gas, ice } /// Enum that enumerates the different planets in our solar system /// and some of their properties. enum Planet { mercury(planetType: PlanetType.terrestrial, moons: 0, hasRings: false), venus(planetType: PlanetType.terrestrial, moons: 0, hasRings: false), // ··· uranus(planetType: PlanetType.ice, moons: 27, hasRings: true), neptune(planetType: PlanetType.ice, moons: 14, hasRings: true); /// A constant generating constructor const Planet( {required this.planetType, required this.moons, required this.hasRings}); /// All instance variables are final final PlanetType planetType; final int moons; final bool hasRings; /// Enhanced enums support getters and other methods bool get isGiant => planetType == PlanetType.gas || planetType == PlanetType.ice; } final yourPlanet = Planet.earth; if (!yourPlanet.isGiant) { print('Your planet is not a "giant planet".'); } mixin Piloted { int astronauts = 1; void describeCrew() { print('Number of astronauts: $astronauts'); } } const oneSecond = Duration(seconds: 1); // ··· Future printWithDelay(String message) async { await Future.delayed(oneSecond); print(message); } Future printWithDelay(String message) { return Future.delayed(oneSecond).then((_) { print(message); }); } Future createDescriptions(Iterable objects) async { for (final object in objects) { try { var file = File('$object.txt'); if (await file.exists()) { var modified = await file.lastModified(); print( 'File for $object already exists. It was modified on $modified.'); continue; } await file.create(); await file.writeAsString('Start describing $object in this file.'); } on IOException catch (e) { print('Cannot create description for $object: $e'); } } } Stream report(Spacecraft craft, Iterable objects) async* { for (final object in objects) { await Future.delayed(oneSecond); yield '${craft.name} flies by $object'; } } Future describeFlybyObjects(List flybyObjects) async { try { for (final object in flybyObjects) { var description = await File('$object.txt').readAsString(); print(description); } } on IOException catch (e) { print('Could not describe object: $e'); } finally { flybyObjects.clear(); } } class Television { /// Use [turnOn] to turn the power on instead. @Deprecated('Use turnOn instead') void activate() { turnOn(); } /// Turns the TV's power on. void turnOn() {...} // ··· } String? name // Nullable type. Can be `null` or string. String name // Non-nullable type. Cannot be `null` but can be string. /// A domesticated South American camelid (Lama glama). /// /// Andean cultures have used llamas as meat and pack /// animals since pre-Hispanic times. /// /// Just like any other animal, llamas need to eat, /// so don't forget to [feed] them some [Food]. class Llama { String? name; /** Feeds your llama [food]. / / The typical llama eats one bale of hay per week. **/ void feed(Food food) { // ... } /// Exercises your llama with an [activity] for /// [timeLimit] minutes. void exercise(Activity activity, int timeLimit) { // ... } } import 'package:lib1/lib1.dart'; import 'package:lib2/lib2.dart' as lib2; // Import only foo. import 'package:lib1/lib1.dart' show foo; // Import all names EXCEPT foo. import 'package:lib2/lib2.dart' hide foo; print(#mysymbol); Symbol symbol = #myMethod; Symbol symbol2 = #<; Symbol symbol2 = #void; var x = 1; var hex = 0xDEADBEEF; var y = 1.1; var exponents = 1.42e5; var s1 = 'Single quotes work well for string literals.'; var s2 = "Double quotes work just as well."; var s3 = 'It\'s easy to escape the string delimiter.'; var s4 = "It's even easier to use the other delimiter."; var s = 'string interpolation'; assert('Dart has $s, which is very handy.' == 'Dart has string interpolation, ' 'which is very handy.'); assert('That deserves all caps. ' '${s.toUpperCase()} is very handy!' == 'That deserves all caps. ' 'STRING INTERPOLATION is very handy!'); var s1 = 'String ' 'concatenation' " works even over line breaks."; assert(s1 == 'String concatenation works even over ' 'line breaks.'); var s2 = 'The + operator ' + 'works, as well.'; assert(s2 == 'The + operator works, as well.'); var s1 = ''' You can create multi-line strings like this one. '''; var s2 = """This is also a multi-line string."""; var s = r'In a raw string, not even \n gets special treatment.'; var 2 = r"In a raw string, not even \n gets special treatment."; var s1 = r''' You can create multi-line strings like this one. '''; var s2 = r"""This is also a multi-line string."""; var record = ('first', a: 2, b: true, 'last'); var record = ('first', a: 2, b: true, 'last'); print(record.$1); // Prints 'first' print(record.a); // Prints 2 print(record.b); // Prints true print(record.$2); // Prints 'last' ({String name, int age}) userInfo(Map json) // ··· // Destructures using a record pattern with named fields: final (:name, :age) = userInfo(json); var list = [1, 2, 3]; var list = [ 'Car', 'Boat', 'Plane', ]; var halogens = {'fluorine', 'chlorine', 'bromine', 'iodine', 'astatine'}; var nobleGases = { 2: 'helium', 10: 'neon', 18: 'argon', }; var s = """This is also a ${foo( "$bar" )} multi-line string."""; var s1 = """multi line \n strings """; var s2 = """multi-line $x strings """; var s3 = """multi-line ${x} strings """; var s1 = 'Unterminated string; var s2 = "Unterminated string; var s3 = r'Unterminated raw string; var s4 = r'Unterminated raw string;