The Go Blog

dart training

bantana
1 July 2018

基本语法

  • A Basic Program
// Define a function
printInteger(int aNumber) {
  print('The number is ${ aNumber }'); // print to console
}

// This is where the app starts executing.
void main() {
  var number = 42; // Declare and initialize a variable.
  printInteger(number); // Call a function
}

result:

The number is 42

// This is a Comment

A single-line comment. Dart also support multi-line and document comments. For details, see Comments.

int

A type. Some of the other built-in types and String, List, and biol.

42

A number literal. Number literal are kind of compile-time constant.

print()

A handy way to display output.

'...' ( or "..." )

A string literal.

Keywords

abstract  | do        | import    | super
as        | dynamic   | in        | switch
async     | else      | interface | sync
async*    | enum      | is        | this
await     | external  | mixin     | true

Variables

var name = 'Bob';
  • Default Value
int lineCount; // default value is null
  • runtimeType
dynamic str = 'hello';
print(str.runtimeType);
print(str.runtimeType.toString());
  • final and count
final name = 'Bob';
finale String nickname = 'Bobby';
  • Number
int // -2^63 to 2^63 - 1 // javascript 2^53 to 2^53 - 1
double // 64 bit , IEEE 754 standard.

// String -> int
var one = int.parse('1');
assert(one == 1)
  • Strings
String str1 = 'hello';
String str2 = "hello";
String strMerge = str1 + str2;
  • Booleans
// Check for an empty string.
var fullname = '';
assert(fullname.isEmpty);
  • Lists

imports

  • 导入标准包
import 'dart:io';
  • 导入外部包
import 'package:mylib/lib.dart';
import 'package:yourlib/lib.dart' as yourlib;
import 'package:mylib1/lib1.dart' show foo;
import 'package:mylib2/lib2.dart' hide foot;
  • 导入本地文件

Related articles