dart基础---->dart语法入门

Dart is an open source, structured programming language for creating complex, browser-based web applications.

first dart demo

main() {
  var d = "Dart";
  String w = "World";
  print("Hello $w! I love $d"); // Hello World! I love Dart
}

string interpolation

void main() {
  var h = "Hello";
  final w = "World";
  print('$h $w'); // Hello World
  print(r'$h $w'); // $h $w

  var helloWorld = "Hello " "World";
  print(helloWorld); // Hello World 
  print("${helloWorld.toUpperCase()}"); // HELLO WORLD
  print("The answer is ${5 + 10}"); // The answer is 15

  var multiline = """
  
"Hello World"
"""; print(multiline); var o = Object(); print(o.toString()); // Instance of 'Object' print("$o"); // // Instance of 'Object' }

Multiline strings ignore first line break following """ and also can contain both single and double quotes.

dart class in use

class Greeter {
  var greeting; // public property
  var _name; // private property
  sayHello() => "$greeting ${this.name}";

  get name => _name; // getter method for _name
  set name(value) => _name = value; // setter method for _name
}

main() {
  var greeter = Greeter();
  greeter.greeting = "Hello ";
  greeter.name = "World";
  print(greeter.sayHello()); // Hello  World
}

Implied interface definitions

Dart has interfaces just like Java and C#, but in Dart, you use the class structure to define an interface. This works on the basis that all classes define an implicit interface on their public members.

class Welcomer {
  printGreeting() => print("Hello ${name}");
  var name;
}

class Greeter implements Welcomer {
  printGreeting() => print("Greetings ${name}");
  var name;
}

void sayHello(Welcomer welcomer) {
  welcomer.printGreeting();
}

main() {
  var welcomer = Welcomer();
  welcomer.name = "Tom";
  sayHello(welcomer); // Hello Tom
  var greeter = Greeter();
  greeter.name = "Tom";
  sayHello(greeter); // Greetings Tom
}

Factory constructors to provide default implementations

abstract class IGreetable {
  String sayHello(String name);
  factory IGreetable() => Greeter();
}

class Greeter implements IGreetable {
  sayHello(name) => "Hello $name";
}

void main() {
  IGreetable myGreetable = IGreetable();
  var message = myGreetable.sayHello("Dart");
  print(message); // Hello Dart
}

Libraries and scope

library "my_library"; // Declares that file is a library
import "../lib/my_other_library.dart"; // Imports another library from a different folder

part "greeter.dart"; // Includes other source files (containing Greeter class)
part "leaver.dart";

greetFunc() { // Defines function in top- level library scope
  var g = new Greeter(); // Uses class from greeter.dart file
  sayHello(g); // Calls function in top-level scope of my_other_library
}

To avoid naming conflicts, you can use as keywords.

import "../lib/my_other_library.dart" as other;

Functions as first-class objects

String sayHello(name) => "Hello $name"; // Declares function using function shorthand
main() {
  var myFunc = sayHello; // Assigns function into variable
  print(myFunc("World")); // Calls function stored in variable
  var mySumFunc = (a, b) => a + b; // Defines anonymous function
  var c = mySumFunc(1, 2); // Calls anonymous function
  print(c); // 
}

Mixins

Mixins 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');
  }
}

class PilotedCraft with Piloted {} // now has the astronauts field as well as the describeCrew() method.

main(List args) {
  var plotedCraft = PilotedCraft();
  plotedCraft.astronauts = 23;
  plotedCraft.describeCrew(); // Number of astronauts: 23
}

To specify that only certain types can use the mixin.

mixin MusicalPerformer on Musician {
  // ···
}

spread operator (...) and the null-aware spread operator (...?)(Dart 2.3)

you can use the spread operator (...) to insert all the elements of a list into another list:

var list = [1, 2, 3];
var list2 = [0, ...list];
assert(list2.length == 4);

If the expression to the right of the spread operator might be null, you can avoid exceptions by using a null-aware spread operator (...?):

var list;
var list2 = [0, ...?list];
assert(list2.length == 1);

collection if and collection for(Dart 2.3)

using collection if to create a list with three or four items in it:

var nav = [
  'Home',
  'Furniture',
  'Plants',
  if (promoActive) 'Outlet'
];

using collection for to manipulate the items of a list before adding them to another list:

var listOfInts = [1, 2, 3];
var listOfStrings = [
  '#0',
  for (var i in listOfInts) '#$i'
];
assert(listOfStrings[1] == '#1');

Optional parameters

Optional parameters can be either named or positional, but not both.

Named parameters

When calling a function, you can specify named parameters using paramName: value. For example:

// define method which parameters with {}
void enableFlags({bool bold, bool hidden}) {...}

// call method
enableFlags(bold: true, hidden: false);

Although named parameters are a kind of optional parameter, you can annotate them with @required to indicate that the parameter is mandatory.

const Scrollbar({Key key, @required Widget child})
Positional parameters

Wrapping a set of function parameters in [] marks them as optional positional parameters:

String say(String from, String msg, [String device]) {
  var result = '$from says $msg';
  if (device != null) {
    result = '$result with a $device';
  }
  return result;
}

assert(say('Bob', 'Howdy') == 'Bob says Howdy');
assert(say('Bob', 'Howdy', 'smoke signal') =='Bob says Howdy with a smoke signal');
Default parameter values

Your function can use = to define default values for both named and positional parameters. The default values must be compile-time constants. If no default value is provided, the default value is null.

String say(String from, String msg, [String device = 'carrier pigeon', String mood]) {
  var result = '$from says $msg';
  if (device != null) {
    result = '$result with a $device';
  }
  if (mood != null) {
    result = '$result (in a $mood mood)';
  }
  return result;
}

assert(say('Bob', 'Howdy') =='Bob says Howdy with a carrier pigeon');

division use / or ~/

assert(5 / 2 == 2.5); // Result is a double
assert(5 ~/ 2 == 2); // Result is an int
assert(5 % 2 == 1); // Remainder

Type test operators

The as, is, and is! operators are handy for checking types at runtime.

if (emp is Person) {
  // Type check
  emp.firstName = 'Bob';
}

Conditional expressions

Dart has two operators that let you concisely evaluate expressions that might otherwise require if-else statements:

  • condition ? expr1 : expr2:

If condition is true, evaluates expr1 (and returns its value); otherwise, evaluates and returns the value of expr2.

var visibility = isPublic ? 'public' : 'private';
  • expr1 ?? expr2

If expr1 is non-null, returns its value; otherwise, evaluates and returns the value of expr2.

// If the boolean expression tests for null, consider using ??.
String playerName(String name) => name ?? 'Guest';

// Slightly longer version uses ?: operator.
String playerName(String name) => name != null ? name : 'Guest';

// Very long version uses if-else statement.
String playerName(String name) {
  if (name != null) {
    return name;
  } else {
    return 'Guest';
  }
}

Cascade notation (..)

Cascades (..) allow you to make a sequence of operations on the same object. In addition to function calls, you can also access fields on that same object. This often saves you the step of creating a temporary variable and allows you to write more fluid code.

// first way
querySelector('#confirm') // Get an object.
  ..text = 'Confirm' // Use its members.
  ..classes.add('important')
  ..onClick.listen((e) => window.alert('Confirmed!'));
  
// second way
var button = querySelector('#confirm');
button.text = 'Confirm';
button.classes.add('important');
button.onClick.listen((e) => window.alert('Confirmed!'));

give an example:

class Person {
  String name;
  int age;

  Person({this.name, this.age});
}

main(List args) {
  Person person = Person()
    ..age = 26
    ..name = "huhx";
  print('${person.age} name ${person.name}'); // 26 name huhx
  person
    ..age = 27
    ..name = "gohuhx";
  print('${person.age} name ${person.name}'); // 27 name gohuhx
}

operator ?.

import 'dart:math';

main(List args) {
  var p = Point(2, 2);
  print(p.y); // 2
  var p2;
  print(p2); // null
  print(p2?.y); // null
}

Getting an object’s type

To get an object’s type at runtime, you can use Object’s runtimeType property, which returns a Type object.

import 'dart:math';

main(List args) {
  var p = Point(2, 2);
  print(p.runtimeType); // Point
  var s = "string";
  print(s.runtimeType); // String
}

Constructors

class Point {
  double x, y;

  Point(double x, double y) {
    // There's a better way to do this, stay tuned.
    this.x = x;
    this.y = y;
  }
}

// better way to construct
class Point {
  double x, y;

  // Syntactic sugar for setting x and y before the constructor body runs.
  Point(this.x, this.y);
}

Extension

Extension methods, introduced in Dart 2.7, are a way to add functionality to existing libraries.

extension NumberParsing on String {
  int parseInt() {
    return int.parse(this);
  }
}

main(List args) {
  print('42'.parseInt()); // 42
}

你可能感兴趣的:(dart基础---->dart语法入门)