在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Hello WorldEvery app has a void main() { print('Hello, World!'); }
VariablesEven in type-safe Dart code, most variables don’t need explicit types, thanks to type inference: var name = 'Voyager I'; var year = 1977; var antennaDiameter = 3.7; var flybyObjects = ['Jupiter', 'Saturn', 'Uranus', 'Neptune']; var image = { 'tags': ['saturn'], 'url': '//path/to/saturn.jpg' };
Read more about variables in Dart, including default values, the Control flow statementsDart supports the usual control flow statements: if (year >= 2001) { print('21st century'); } else if (year >= 1901) { print('20th century'); } for (var object in flybyObjects) { print(object); } for (int month = 1; month <= 12; month++) { print(month); } while (year < 2016) { year += 1; }
Read more about control flow statements in Dart, including FunctionsWe recommend specifying the types of each function’s arguments and return value: int fibonacci(int n) { if (n == 0 || n == 1) return n; return fibonacci(n - 1) + fibonacci(n - 2); } var result = fibonacci(20);
A shorthand flybyObjects.where((name) => name.contains('turn')).forEach(print);
Besides showing an anonymous function (the argument to Read more about functions in Dart, including optional parameters, default parameter values, and lexical scope. CommentsDart comments usually start with // 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. */
Read more about comments in Dart, including how the documentation tooling works. ImportsTo access APIs defined in other libraries, use // 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';
Read more about libraries and visibility in Dart, including library prefixes, ClassesHere’s an example of a class with three properties, two constructors, and a method. One of the properties can’t be set directly, so it’s defined using a getter method (instead of a variable). class Spacecraft { String name; DateTime launchDate; // 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); int get launchYear => launchDate?.year; // read-only non-final property // Method. void describe() { print('Spacecraft: $name'); if (launchDate != null) { int years = DateTime.now().difference(launchDate).inDays ~/ 365; print('Launched: $launchYear ($years years ago)'); } else { print('Unlaunched'); } } }
You might use the var voyager = Spacecraft('Voyager I', DateTime(1977, 9, 5)); voyager.describe(); var voyager3 = Spacecraft.unlaunched('Voyager III'); voyager3.describe();
Read more about classes in Dart, including initializer lists, optional InheritanceDart has single inheritance. class Orbiter extends Spacecraft { num altitude; Orbiter(String name, DateTime launchDate, this.altitude) : super(name, launchDate); }
Read more about extending classes, the optional MixinsMixins are a way of reusing code in multiple class hierarchies. The following class can act as a mixin: class Piloted { int astronauts = 1; void describeCrew() { print('Number of astronauts: $astronauts'); } }
To add a mixin’s capabilities to a class, just extend the class with the mixin. class PilotedCraft extends Spacecraft with Piloted { // ··· }
Read more about mixins. Interfaces and abstract classesDart has no class MockSpaceship implements Spacecraft { // ··· }
Read more about implicit interfaces. You can create an abstract class to be extended (or implemented) by a concrete class. Abstract classes can contain abstract methods (with empty bodies). abstract class Describable { void describe(); void describeWithEmphasis() { print('========='); describe(); print('========='); } }
Any class extending Read more about abstract classes and methods. AsyncAvoid callback hell and make your code much more readable by using const oneSecond = Duration(seconds: 1); // ··· Future<void> printWithDelay(String message) async { await Future.delayed(oneSecond); print(message); } The method above is equivalent to: Future<void> printWithDelay(String message) { return Future.delayed(oneSecond).then((_) { print(message); }); }
As the next example shows, Future<void> createDescriptions(Iterable<String> objects) async { for (var 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'); } } }
You can also use Stream<String> report(Spacecraft craft, Iterable<String> objects) async* { for (var object in objects) { await Future.delayed(oneSecond); yield '${craft.name} flies by $object'; } }
Read more about asynchrony support, including async functions, ExceptionsTo raise an exception, use if (astronauts == 0) { throw StateError('No astronauts.'); }
To catch an exception, use a try { for (var 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(); }
Note that the code above is asynchronous; Read more about exceptions, including stack traces, |
请发表评论