人社局网站群建设工作方案,常州网页模板建站,网站logo设计免费版在线,专业的网站设计制作在Dart中#xff0c;函数为 一等公民#xff0c;可以作为参数对象传递#xff0c;也可以作为返回值返回。 函数定义
// 返回值 (可以不写返回值#xff0c;但建议写)、函数名、参数列表
showMessage(String message) {//函数体print(message);
}void showMessage(String m…在Dart中函数为 一等公民可以作为参数对象传递也可以作为返回值返回。 函数定义
// 返回值 (可以不写返回值但建议写)、函数名、参数列表
showMessage(String message) {//函数体print(message);
}void showMessage(String message) {print(message);
} 可选参数
位置参数用方括号[]表示参数根据位置传递给函数
void showNewMessage(int age, [String? birthday]) {if (birthday ! null birthday.isNotEmpty) {print(birthday$birthday);} else {print(age$age);}
}命名参数用大括号表示参数根据名称传递给函数
// {}命名参数required 表示该参数是必须的
void showFirstMessage({required String content, int? time}) {
} 上面?问号修饰符 是 nullable修饰符用于修饰参数表示此参数可传可不传。 ? 操作符可选参数如果不为null则执行
//? 操作符可选参数如果不为null则执行
String? content;
int size content?.length ?? 0;
//?? 操作符表示如果表达式为空值时提供一个默认值
int size content?.length ?? 0; 如下面Container的构造函数使用了命名参数 Container({super.key,this.alignment,this.padding,this.color,this.decoration,this.foregroundDecoration,double? width,double? height,BoxConstraints? constraints,this.margin,this.transform,this.transformAlignment,this.child,this.clipBehavior Clip.none,})
required : 对命名可选参数添加required关键字让对应的命名可选参数变成必传参数
MaterialPageRoute({required this.builder,super.settings,this.maintainState true,super.fullscreenDialog,super.allowSnapshotting true,super.barrierDismissible false,})
箭头/匿名函数
箭头函数用于定义单行函数用 箭头符号() 分隔参数和函数体并省略了函数体的大括号
int sum(int a, int b) a b; 函数作为参数
String append(String title, String content) $title$content;void testFunction(String title, String content, Function append) {String result append(title, title);print(result);
}函数作为返回值
int sum(int a, int b) a b;Function testReturnFunction() {return sum;
}