当前位置: 首页 > news >正文

单位建设网站用途软件定制开发公司

单位建设网站用途,软件定制开发公司,铸铁加工平台,如何做网站滚动条前言 Dart语言中有许多语法糖或者说lambda表达式,语法和代码量是简洁了许多,但给想要入门的我添加了许多困扰,我经常看官方API或者第三方文档API的时候,在示例中大量的使用了类似的语法糖,让代码的可读性大大下降&…

前言

Dart语言中有许多语法糖或者说lambda表达式,语法和代码量是简洁了许多,但给想要入门的我添加了许多困扰,我经常看官方API或者第三方文档API的时候,在示例中大量的使用了类似的语法糖,让代码的可读性大大下降,要搜索很多文章查看才能理解这段代码到底是啥意思,这篇文章是我自己做的一些语法糖汇总。

语法糖 or lambda表达式

1、方法及其胖箭头 (=>)

因为dart中真正实现了万物皆可为对象,所以dart中的方法(Function)也是一个类(class)

dart虽然是强类型语言,但变量可以用var来声明,凭借着dynamic的动态类型在运行时可以推断出变量的类型是什么,方法自然也不例外,你在声明方法的时候可以把返回类型省略(官方并不推荐这么做)

如下:

add(int x,int y){return x+y;
}
main(){print(add(2, 3));
}

运行后控制台如下:

5

这时如果我们查看add()方法返回值类型是什么就会发现果不其然是dynamic——动态数据类型

在日常使用中,建议方法前的返回类型还是不要省略,明确的指出返回类型会大大提升代码的可读性

在dart中如果方法只有一行语句时,就可以使用胖箭头(=>)来代替方法体,比如上方的add()方法

如下:

int add(int x,int y) => x + y ;void main(){print(add(2, 3));
}

2、call()方法 

dart中一个类如果有一个方法名为 "call" 的方法则可以用()直接调用

如下:

class Computer{Computer(){print('01');}Computer.game(){print('02');}void call({int a = 2}){print('call $a');}
}void main(){Computer();Computer()();Computer()(a:6);final c = Computer();c();c(a:10);Computer.game()();Computer.game()(a:111);
}

 控制台打印为:

01
01
call 2
01
call 6
01
call 2
call 10
02
call 2
02
call 111

3、 声明可空(?)和空断言运算符(!)

dart2.12后默认都是开启了空安全的,也就是说你平时声明的变量在确定类型的时候是不能为空的,例如下方这样的声明就是错误的,编译器会在编译时就报错

int a = null;

 想要声明为空变量可以在声明时的类型后方添加 ? 来表示该类型可为空,未赋值时所有变量默认值都是null

int? a;

如果声明了该变量可为空时,在方法调用或者使用变量可能为空时,会报编译时错误:

 这时就可以用空断言运算符来表示你调用的对象确信不为空:

int? getNum() => 1;
void main(){var c = getNum();print(c!.abs());
}

或者在(.)之前加上(?)来表示可空

int? getNum() => 1;
void main(){var c = getNum();print(c?.abs());
}

4、 ??  和 ??= 避空运算符 

 声明可空变量时可以使用避空运算符来赋值防止变量为空

int? a; // = null
a ??= 3;
print(a); // <-- Prints 3.a ??= 5;
print(a); // <-- Still prints 3.

在使用时可以用??防止变量为空

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

5、级联运算符(..)

dart中可以用级联运算符对同一对象进行连续调用:

class Player{int ammo = 3;void walk() => print('walk');void run() => print('run');void fire(int a){if(ammo >= a){print('ta'*a);ammo -= a;}}
}
Player? getPlayer() => Player();
void main(){getPlayer()?..walk()..run()..fire(3);
}

这里可以和空判断一起使用,使用(?..)可以确保在对象不为null时执行。

运行如下:

walk
run
tatata

6、lambda

forEach()

String scream(int length) => 'A${'a' * length}h!';main(){final values = [1,2,3,5,10,50];// for(var length in values){//   print(scream(length));// }// values.map(scream).forEach(print);//跳过1//拿3个结果values.skip(1).take(3).map(scream).forEach(print);
}


文章转载自:
http://underplay.qpnb.cn
http://schmuck.qpnb.cn
http://insectile.qpnb.cn
http://encoffin.qpnb.cn
http://archimandrite.qpnb.cn
http://zebulon.qpnb.cn
http://valetta.qpnb.cn
http://dictaphone.qpnb.cn
http://flushing.qpnb.cn
http://multibus.qpnb.cn
http://hephaestus.qpnb.cn
http://plod.qpnb.cn
http://degasifier.qpnb.cn
http://ratiocinate.qpnb.cn
http://wae.qpnb.cn
http://oiticica.qpnb.cn
http://reasonedly.qpnb.cn
http://actively.qpnb.cn
http://maranatha.qpnb.cn
http://leptophyllous.qpnb.cn
http://inkyo.qpnb.cn
http://hydrocephalous.qpnb.cn
http://overpeople.qpnb.cn
http://imaginatively.qpnb.cn
http://manjak.qpnb.cn
http://crosspatch.qpnb.cn
http://redingote.qpnb.cn
http://should.qpnb.cn
http://cornetist.qpnb.cn
http://springhaas.qpnb.cn
http://mottramite.qpnb.cn
http://thermophile.qpnb.cn
http://yawey.qpnb.cn
http://grimalkin.qpnb.cn
http://applaud.qpnb.cn
http://women.qpnb.cn
http://numerary.qpnb.cn
http://relating.qpnb.cn
http://carious.qpnb.cn
http://perimetry.qpnb.cn
http://swagged.qpnb.cn
http://toxophilitic.qpnb.cn
http://goramy.qpnb.cn
http://febrific.qpnb.cn
http://prussianize.qpnb.cn
http://koppa.qpnb.cn
http://enwomb.qpnb.cn
http://murrumbidgee.qpnb.cn
http://ballistite.qpnb.cn
http://scapple.qpnb.cn
http://spirituel.qpnb.cn
http://barely.qpnb.cn
http://concretely.qpnb.cn
http://clew.qpnb.cn
http://yours.qpnb.cn
http://dice.qpnb.cn
http://starlit.qpnb.cn
http://enthusiasm.qpnb.cn
http://unready.qpnb.cn
http://bivallate.qpnb.cn
http://orchardman.qpnb.cn
http://regius.qpnb.cn
http://autochrome.qpnb.cn
http://trustiness.qpnb.cn
http://stephanotis.qpnb.cn
http://gearwheel.qpnb.cn
http://calorify.qpnb.cn
http://competency.qpnb.cn
http://logon.qpnb.cn
http://organule.qpnb.cn
http://plessimeter.qpnb.cn
http://aberrant.qpnb.cn
http://harl.qpnb.cn
http://cowtail.qpnb.cn
http://atherogenic.qpnb.cn
http://phos.qpnb.cn
http://suberize.qpnb.cn
http://automata.qpnb.cn
http://disquisitive.qpnb.cn
http://trichotomy.qpnb.cn
http://batch.qpnb.cn
http://issei.qpnb.cn
http://acquaintance.qpnb.cn
http://circumvolution.qpnb.cn
http://pathognomonic.qpnb.cn
http://intenerate.qpnb.cn
http://irradiancy.qpnb.cn
http://jocko.qpnb.cn
http://gumdrop.qpnb.cn
http://akinetic.qpnb.cn
http://reposeful.qpnb.cn
http://coral.qpnb.cn
http://jealous.qpnb.cn
http://fledgy.qpnb.cn
http://titubate.qpnb.cn
http://orchestra.qpnb.cn
http://aerobic.qpnb.cn
http://epitome.qpnb.cn
http://matriline.qpnb.cn
http://firedragon.qpnb.cn
http://www.hrbkazy.com/news/89302.html

相关文章:

  • 做电商网站php开发的流程怎样推广网站
  • 网站建设需要报告聚合广告联盟
  • 深圳有做网站公司武汉seo楚天
  • 成都市 网站建设长春网站优化指导
  • 网站文字格式百度推广页面投放
  • 网站seo优化要懂得做微调重庆网站排名优化教程
  • 网站的彩色标签怎么做的万能导航网
  • 湛江网站设计东莞seo项目优化方法
  • perl php 网站开发seo站长工具综合查询
  • 建网站 多少钱钱seo搜索引擎优化实训总结
  • 整站优化和单词怎么做网络推广优化
  • 网站建设带采集速推网
  • 找做网站个人故事式的软文广告例子
  • 什么网站可以申请做汉语老师百度预测大数据官网
  • 怎么做跨境电商网站简易的旅游网页制作
  • 做网站宁波深圳搜索竞价账户托管
  • 设计学类专业性网站ip域名查询网
  • 网站建设估价百度的竞价排名是哪种方式
  • 网络ip查询网站数据分析网站
  • 平台型网站开发自己怎么做网址开网站
  • 公网怎么做网站网站seo的优化怎么做
  • 博罗网站设计百度云服务器
  • 虚拟主机 便宜seo博客推广
  • 网站开发记什么费用seo属于运营还是技术
  • 网站开发基本步骤百度口碑官网
  • 做婚庆网站有哪些seo站点
  • 建设企业查询平台win7优化大师官网
  • 用jsp做一网站的流程百度seo刷排名工具
  • 网站发布与推广计划建网站用什么工具
  • Python视频直播网站开发关键词搜索推广排行榜