Dart-基本类型

1、字符串表达式 

描述:${expression} ,如果表达式是变量,可以省略{}

示例:'${3 + 2}'  '5'  

          '${"word".toUpperCase()}'      'WORD'  

          '$myObject'                The value of myObject.toString()

2、null空值感知运算符

??= 赋值运算符,仅在该变量当前为null时才为该变量赋值.

int  a;// The initial value of a is null.

a??=3;

print(a);// <-- Prints 3.

??,表达式的值为null,右侧求值并返回该表达式

print(1??3);// <-- Prints 1.

print(null??12);// <-- Prints 12.

myObject?.someProperty 等价于(myObject!=null)?myObject.someProperty:null

也可以链式操作myObject?.someProperty?.someMethod()

3、集合类型

(1)类型推断

dart内建集合类型包括lists, maps, and sets;

final  aListOfStrings=['one','two','three'];

final  aSetOfStrings={'one','two','three'};

final  aMapOfStringsToInts={'one':1,'two':2,'three':3,};

推断为:

final  aListOfInts=<int>[];

final  aSetOfInts=<int>{};

final  aMapOfIntToDouble=<int,double>{};

子类型初始化,但希望类型为父类型:final  aListOfBaseType=<BaseType>[SubType(),SubType()];

4、=>

5、链式操作

myObject.someMethod() 返回someMethod方法的结果

myObject..someMethod()  返回myObject对象

因此可以

querySelector('#confirm')

..text='Confirm'

..classes.add('important')

..onClick.listen((e)=>window.alert('Confirmed!'));

6、函数

(1)函数参数

参数包括位置参数和命名参数,位置参数如下:

int sumUp(int a, int b, int c) {

int sumUpToFive(int a, [int b, int c, int d, int e]) {  中括号表示可选,默认值是null

int sumUpToFive(int a, [int b = 2, int c = 3, int d = 4, int e = 5]) {  设置默认值

命名参数如下:

void printName(String firstName, String lastName, {String suffix}) { //大括号表示可选命名参数

void printName(String firstName, String lastName, {String suffix = ''}) { 设置默认值


7、类

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。