一、运算符
算数运算符
关系运算符
逻辑运算符
赋值运算符
三目运算符
var a;
var b = a ?? 10;//若a为空,把10赋值给a
print(b);
//output:10
var a=22;
var b = a ?? 10;
print(b);
//output:22
二、条件语句
if(){
}
else{
}
switch(sex){
case "man":
print("man");
break;
case "woman":
print("woman");
break;
default:
print("erro");
break;
}
三、类型转换
1.Number与String类型之间的转换
//String->int
String str = '123';
var myNum = int.parse(str);
print(myNum is int);
//output:true
//浮点型
String str = '123.1';
var myNum = int.parse(str);
print(myNum is int);
//output:报错!
var myNum = double.parse(str);
print(myNum is double);
//output:true
//转换类型时,建议用double,因为无论是整型还是浮点型,都可以转换成浮点型!
//如果服务器返回为空,其实double还是会报错
//解决:
try {
var myNum = double.parse(price);
print(myNum);
print(myNum is double);
}catch(err){
print(0);
}
//Number->String
var myNum = 12;
var str = myNum.toString();
print(str is Stirng);
//output:true
2.其他类型转换成Booleans类型
//isEmpty:判断字符串是否为空
String->Booleans
var str = 'xxx';
if(str.isEmpty){
print('str空');
}else{
print('str不为空');
}
//int->Booleans
var myNum = 123;
if(myNum==0){//myNum==null
print('0');
}else{
print('非0');
}