Dart-變量

dart基礎(chǔ)類型包括三大類型

  • Number (數(shù)字類型)
    • int
    • double
  • Boolean
  • String
//基礎(chǔ)變量聲明
int age = 18;
double height = 175.2;
bool done = false;
String name = "高富帥";

//推導(dǎo)類型
var oneInt=1;
assert(oneInt.runtimeType is int);

var oneString="1";
assert(oneString.runtimeType is String);

var oneBool=false;
assert(oneBool.runtimeType is bool);

final

final 只允許被賦值一次,賦值以后就不能在被set
只能get

const

定義常量使用,定義以后就不能再被改變

const String  menu="MENU";

常用方式

 //String轉(zhuǎn)int
 int one=int.parse("1");
 assert(one==1);

 //String轉(zhuǎn)double
 double two=double.parse("1.1");
 assert(two==1.1);

 //double轉(zhuǎn)String
 String three=1.toString();
 assert(three=="1");

 //double轉(zhuǎn)String
 String four=1.2.toString();
 assert(four=="1.2");

 //取小數(shù)點(diǎn)后2位數(shù)(四舍五入)
 String five=12.185421.toStringAsFixed(2);
 assert(five=="12.19");
 
 //小寫轉(zhuǎn)大寫
 var str = ' foo';
 var str2 = str.toUpperCase();

注意

1,轉(zhuǎn)義字符

當(dāng)String中有轉(zhuǎn)義字符時(shí),想要在輸出的時(shí)候保持轉(zhuǎn)義格式,可以在字符串之前加一個(gè) " r "

String value = r"this is \n string \t value";

2,?? 與 ??=

  • A ?? B

如果 A 非null 就取A,否則就取 B

String key;
String value=key??"test";
assert(value=="test");
  • A ??= B

如果 A 為null 就把B 賦值給A

String key;
String value=key??="test";
assert(key=="test");
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容