当前位置:网站首页>Flutter dart语言特点总结
Flutter dart语言特点总结
2022-06-28 15:14:00 【明似水】
前言
最近用flutter做了一个项目并且顺利上架,总觉得自己对flutter的编译原理、语法基础了解得还不够到位,接下来要对flutter总结一番,分享一些自己的项目经验。
一、Dart语言简介
1、学习dart官方文档
2、是一门强类型语言
3、是一个单线程的语言
4、类型注释是可选的,Dart可以推断类型
5、支持泛型类型
6、程序都必须具有顶级main()功能,该功能用作应用程序的入口点
7、接口可以有函数,类和接口是统一的,都是接口
二、Dart语言变量和常量
1、变量
// 官方建议使用var
var aa = 'hello word' ;
dynamic aa = 'hello word' ;
String aa = 'hello word' ;
注意:
1、未初始化的变量的初始值为null
2、不具备关键字public,protected和private
3、私有方法用(_)开头,其他的标识公开方法。
2、常量
final aa = 'Bob' ;//
const aa = 'Bob' ;//
const在类级别,要加static const
const构造两个相同的编译时常量会产生同一个的实例
var a = const ImmutablePoint(1, 1);
var b = const ImmutablePoint(1, 1);
assert(identical(a, b)); // They are the same instance!
注意:
1、使用static关键字实现类范围的变量和方法( 对于常用或广泛使用的实用程序和功能,请考虑使用顶级函数而不是静态方法)。
2、final 要求变量只能初始化一次,并不要求赋的值一定是编译时常量,可以是常量也可以不是。而 const 要求在声明时初始化,并且赋值必需为编译时常量,(编译时常量:字面量(如数字、bool、字符串、List的字面量形式)、其它常量 或者 常量的算术运算,也可以是这些的组合形式(递归要求),简单地说常量就是可以在编译时确定的值。)
3、final 是惰性初始化,即在运行时第一次使用前才初始化。而 const 是在编译时就确定值了。
4、const 可以修饰变量,也可以修饰值(value)。而 final 只用来修饰变量。
3、late关键字
late的作用:
1、将您的项目迁移到零安全。
在声明初始化的不可为空变量时可以使用late 修饰符。
late String title;
void getTitle(){
title = 'Default';
print('Title is $title');
}
注意:在使用变量之前的后期确保变量稍后必须初始化。否则在使用变量时可能会遇到运行时错误。
2、延时初始化一个变量。
该变量可能不需要,并且初始化它的成本很高。
您正在初始化一个实例变量,它的初始化程序需要访问它。
// This is the program's only call to _getResult().
late String result = _getResult(); // Lazily initialized.
在上面的示例中,如果从未使用过变量,则永远不会调用成本更高的 _getResult() 函数。
假设_getResult()是计算该结果的非常重要的函数。 但是,如果我们将它分配给任何变量而不延时,那么_getResult()即使我们不使用它,每次都会执行。
没有 late关键字
String result = _getResult();
在上面的代码中,result从未使用过,但_getResult()依旧会被执行。
使用 late关键字
late String result = _getResult();
在上面的代码中_getResult()没有被执行,因为变量result从未使用过发现了没,它是使用late修饰符声明的。
3、您可以删除许多initState/constructor调用!
以前,如果我们要创建AnimationController,则必须在initState构造函数中完成,因为this, 所需的vsync只能从方法中访问。
AnimationController anim1;
@override
void initState() {
super.initState();
anim1 = AnimationController(vsync: this, duration: Duration(seconds: 1))..forward();
}
现在,我们可以这样写:
late AnimationController anim = AnimationController(vsync: ``this``, duration: Duration(seconds: 1))..forward();
这为您节省了 6 行代码
您可以使用它从计算方法中获取初始值,或设置取决于动态值的任何默认值。
您还可以创建构建器方法,并调用它们:
late AnimationController anim1 = createAnim(seconds: 1, play: true);
late AnimationController anim2 = createAnim(seconds: 2);
late AnimationController anim3 = createAnim(seconds: 3);
AnimationController createAnim({required int seconds, bool play = false}) {
final c = AnimationController(vsync: this, duration: Duration(seconds: seconds));
if(play) c.forward();
c.addListener(() => setState((){}));
return c;
}
不需要initState,也不@override使得每一行都有意义。
其他的总结后续持续更新!
END.
边栏推荐
- R language ggplot2 visualization: use the patchwork package (directly use the plus sign +) to horizontally combine a ggplot2 visualization result and a plot function visualization result to form a fin
- R language ggplot2 visualization: use the patchwork package to horizontally form two ggplot2 visualization results into a new result visualization combination diagram (using the | symbol)
- MongoDB 在腾讯零售优码中的应用
- 新零售线下店逆势起飞,通膨乌云下的消费热情
- 验证回文串
- 一文教你快速生成MySQL数据库关系图
- Vector explanation + topic
- 开源大咖说 - Linus 与 Jim 对话中国开源
- Experiment 6 8255 parallel interface experiment [microcomputer principle] [experiment]
- R语言ggplot2可视化:使用patchwork包将两个ggplot2可视化结果纵向堆叠起来(stacking)形成组合图、一个可视化结果堆叠在另外一个可视化结果上
猜你喜欢

使用Karmada实现Helm应用的跨集群部署

Curve 替换 Ceph 在网易云音乐的实践

2022年最新PyCharm激活破解码永久_详细安装教程(适用多版本)
Technical trendsetter

MIPS assembly language learning-01-sum of two numbers, environment configuration and how to run
![Experiment 6 8255 parallel interface experiment [microcomputer principle] [experiment]](/img/70/394ccf6e08a0774acade1eb1b8bf00.png)
Experiment 6 8255 parallel interface experiment [microcomputer principle] [experiment]
![[C language] how to generate normal or Gaussian random numbers](/img/31/964e0922e698a3746599b840501cdf.png)
[C language] how to generate normal or Gaussian random numbers

GCC efficient graph revolution for joint node representationlearning and clustering

MongoDB 在腾讯零售优码中的应用

How to build a 100000 level QPS large flow and high concurrency coupon system from zero
随机推荐
Case driven: a detailed guide from getting started to mastering shell programming
[spatial & single cellomics] phase 1: Study on PDAC tumor microenvironment by single cell binding spatial transcriptome
Functools: high order functions and operations on callable objects (continuous updating ing...)
智能化转型被加速,企业需要新的工具箱
Seata数据库中出现以下问题要怎么解决啊?
验证回文串
The best time to buy and sell stocks
信创操作系统--麒麟Kylin桌面操作系统 (项目十 安全中心)
Steve Jobs of the United States, died; China jobs, sold
GCC efficient graph revolution for joint node representationlearning and clustering
SAP mts/ato/mto/eto topic 9: front and back desk operation in m+m mode, strategy 50, preparation of raw materials and semi-finished products in advance
R language ggplot2 visualization: use the patchwork package (directly use the plus sign +) to horizontally combine a ggplot2 visualization result and a plot function visualization result to form a fin
MIPS汇编语言学习-01-两数求和以及环境配置、如何运行
Spacetutorial (continuous updating...)
从五大能力到 “1+5+N”,华为让政企转型更稳健
从莫高窟到太平洋,海量数据找到了新家园
C#/VB.NET 将PDF转为Excel
美国乔布斯,殁了;中国乔布斯,卖了
R language ggplot2 visualization: the patchwork package horizontally combines a ggplot2 visualization result and a plot function visualization result to form a final result graph, aligns the two visua
环保产品“绿色溢价”高?低碳生活方式离人们还有多远