当前位置:网站首页>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.
边栏推荐
- ROS知识点——ROS创建工作空间
- Vscode writes markdown file and generates pdf
- 浪潮网络步步为赢
- Cross cluster deployment of helm applications using karmada
- With a return of 5000 times, the South African newspaper invested in Tencent to make a province
- 如何从零搭建10万级 QPS 大流量、高并发优惠券系统
- S2b2c system website solution for kitchen and bathroom electrical appliance industry: create s2b2c platform Omni channel commercial system
- code snippet
- Experiment 6 8255 parallel interface experiment [microcomputer principle] [experiment]
- Facebook出手!自适应梯度打败人工调参
猜你喜欢

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

High "green premium" of environmental protection products? How far is the low-carbon lifestyle from people

坐拥1200亿,她又要IPO敲钟了

Vscode writes markdown file and generates pdf

GCC efficient graph revolution for joint node representationlearning and clustering
DBMS in Oracle_ output. put_ Line output problem solving process

Gbase Nantah General Motors appears at the 6th World Intelligence Conference

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

MIPS汇编语言学习-01-两数求和以及环境配置、如何运行

Successful cases of rights protection of open source projects: successful rights protection of SPuG open source operation and maintenance platform
随机推荐
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
法兰克福地区目前支持sql了吗?
ROS knowledge points - ROS create workspace
[C language] how to implement plural types
Longest continuous sequence
ROS知识点——使用VScode搭建ROS开发环境
R language ggplot2 visualization: use the patchwork package to stack two ggplot2 visualization results vertically to form a composite diagram, and stack one visualization result on the other visualiza
云杉网络DeepFlow帮助5G核心网和电信云构建可观测性
MySQL主从切换的超详细步骤
Successful cases of rights protection of open source projects: successful rights protection of SPuG open source operation and maintenance platform
MIPS汇编语言学习-01-两数求和以及环境配置、如何运行
信创操作系统--麒麟Kylin桌面操作系统 (项目十 安全中心)
R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses regression coefficients and their standard errors to calculate the
智能化转型被加速,企业需要新的工具箱
当下不做元宇宙,就像20年前没买房!
Complete model training routine (I)
Combined sum leetcode
go-zero 微服务实战系列(七、请求量这么高该如何优化)
How to solve the following problems in the Seata database?
3. caller service call - dapr