voidmain(){
String str ="This is my first proj";print(str *5);// This is my first projThis is my first projThis is my first projThis is my first projThis is my first projprint(str[5]);// i}
插值表达式 ${expression}
voidmain(){
String name ='jack';int age =20;
String addr ='四川成都';
String information ='$name今年$age岁 来自${addr}';print(information);// jack今年20岁 来自四川成都}
var studyPlan ={'first':'Dart','second':'Flutter'};// 创建普通map
var studyPlan =const{'first':'Dart','second':'Flutter'};// 创建一个不可变map
var studyPlan =newMap();// 使用构造函数创建
if…elseif…elseif…else这个和java控制语句相同 ,唯一一个不同的是switch … case 中可以使用continue 跳转到指定语句
voidmain(){
String myHobby ="jog";switch(myHobby){case"game":print("my hobby is game");break;case"jog":print("my hobby is jog");continue Prefer;case"red":print("my hobby is red");break;
Prefer:default:print("I like study different languages, haha!");}// print out :// my hobby is jog// I like study different languages, haha!}
voidmain(){
String info =getPersonInfo('Lili','18');// error: The argument type 'String' can't be assigned to the parameter type 'int'.print(info);}
String getPersonInfo (String name,int age){return"This girl's name is $name,she is $age years old";}
如上你如果调用时参数类型不同就无法编译通过
voidmain(){
String info =getPersonInfo('Lili','18');print(info);// This girl's name is Lili,she is 18 years old}
String getPersonInfo ( name, age){return"This girl's name is $name,she is $age years old";}
voidmain(){
String info =getPersonInfo('Lili',18);print(info);// This girl's name is Lili,she is 18 years old}/*
方法 getPersonInfo 的完整写法
String getPersonInfo(Sting name, int age){
return "This girl's name is $name,she is $age years old";
}
*/
getPersonInfo ( name, age)=>"This girl's name is $name,she is $age years old";
voidmain(){getPersonInfo('Lili');// This girl's name is Lili, she is null years old and she lives in null getPersonInfo('Lili',age:18);// This girl's name is Lili, she is 18 years old and she lives in null getPersonInfo('Lili',age:18,addr:'Chengdu, Sichuan, China');// This girl's name is Lili, she is 18 years old and she lives in Chengdu, Sichuan, China }
getPersonInfo ( String name,{int age ,String addr})=>print("This girl's name is $name, she is $age years old and she lives in $addr ");
看过了可选命名参数,可选位置参数就不言而喻了(就是按参数位置下标进行实参–形参一一对应)
voidmain(){getPersonInfo("Lili");// This girl's name is Lili, she is null years old and she lives in null getPersonInfo("Lili",18,"Chengdu, Sichuan, China");// This girl's name is Lili, she is 18 years old and she lives in Chengdu, Sichuan, China }
getPersonInfo ( String name,[int age ,String addr])=>print("This girl's name is $name, she is $age years old and she lives in $addr ");
还有一个可选参数必选放到必选参数后面(这个同ES6 的不定参数规则完全相同呵)
6.3 默认参数值 (使用 ‘=’ 指定)
voidmain(){getPersonInfo("Lili");// This girl's name is Lili, she is 18 years old and she lives in Chengdu, Sichuan, China}
getPersonInfo ( String name,[int age =18,String addr ="Chengdu, Sichuan, China"])=>print("This girl's name is $name, she is $age years old and she lives in $addr ");
6.4 方法对象
1)方法可以作为对象赋值给其他变量
2)方法可以作为我参数传递给其他方法
voidmain(){
Function func = printPersonalInfo;func();// Hello world!
var studentList =["Binbin","Tingting","Mingmint"];// 将getEachFromList方法作为参数传递个forEach;进行元素打印
studentList.forEach(getEachFromList);// Binbin// Tingting// Mingmintprint(powerOperator(studentList,powerFn));// [BinbinBinbinBinbin, TingtingTingtingTingting, MingmintMingmintMingmint]}printPersonalInfo()=>print("Hello world!");getEachFromList(val)=>print(val);powerOperator(List<String> list,String powerFn(str)){for(var i =0;i< list.length; i++){
list[i]=powerFn(list[i]);}return list;}
String powerFn(str)=> str*3;
6.5 匿名方法 (anonymous)
voidmain(){
var fn =(){print("hello dart !");};
fn ();// hello dart !// 定义一个自动运行方法,同js (function($){}(window.document))()((){print("I am an automated methed!");// I am an automated methed!})();
var studentList =["Binbin","Tingting","Mingmint"];print(powerOperator(studentList,(str)=> str *3));// [BinbinBinbinBinbin, TingtingTingtingTingting, MingmintMingmintMingmint]}printPersonalInfo()=>print("Hello world!");getEachFromList(val)=>print(val);powerOperator(List<String> list,String powerFn(str)){for(var i =0;i< list.length; i++){
list[i]=powerFn(list[i]);}return list;}
请发表评论