当前位置:网站首页>Summary of language features of fluent dart
Summary of language features of fluent dart
2022-06-28 15:23:00 【Bright like water】
Preface
Recently, with flutter Made a project and successfully put it on the shelves , Always feel right flutter The compilation principle of 、 The basic knowledge of grammar is not enough , The next step is to flutter Sum it up , Share some of your own project experience .
One 、Dart Introduction to language
1、 Study dart Official documents
2、 Is a strong type of language
3、 It's a single thread language
4、 Type annotations are optional ,Dart You can infer the type
5、 Supports generic types
6、 All programs must have a top level main() function , This function serves as the entry point for the application
7、 Interfaces can have functions , Classes and interfaces are unified , It's all interfaces
Two 、Dart Language variables and constants
1、 Variable
// Official recommended use var
var aa = 'hello word' ;
dynamic aa = 'hello word' ;
String aa = 'hello word' ;
Be careful :
1、 The initial value of an uninitialized variable is null
2、 No keywords public,protected and private
3、 Private methods use (_) start , Other identity disclosure methods .
2、 Constant
final aa = 'Bob' ;//
const aa = 'Bob' ;//
const At the class level , To add static const
const Constructing two identical compile time constants will produce the same instance
var a = const ImmutablePoint(1, 1);
var b = const ImmutablePoint(1, 1);
assert(identical(a, b)); // They are the same instance!
Be careful :
1、 Use static Keywords implement class wide variables and methods ( For commonly used or widely used utilities and functions , Consider using top-level functions instead of static methods ).
2、final The variable is required to be initialized only once , It is not required that the assigned value must be a compile time constant , It can be constant or not . and const Requires initialization at declaration time , And the assignment must be a compile time constant ,( Compile time constants : Literal ( Like numbers 、bool、 character string 、List The literal form of )、 Other constants perhaps Constant arithmetic operation , It can also be a combination of these ( Recursive requirements ), Simply put, constants are values that can be determined at compile time .)
3、final It's lazy initialization , That is, it is initialized before the first use at runtime . and const The value is determined at compile time .
4、const You can modify variables , You can also decorate values (value). and final Only used to decorate variables .
3、late keyword
late The role of :
1、 Migrate your project to zero security .
When declaring an initialized non nullable variable, you can use late Modifier .
late String title;
void getTitle(){
title = 'Default';
print('Title is $title');
}
Be careful : Make sure that the variable must be initialized later before using it . Otherwise, you may encounter runtime errors when using variables .
2、 Delay initializing a variable .
This variable may not require , And the cost of initializing it is very high .
You are initializing an instance variable , Its initializer needs to access it .
// This is the program's only call to _getResult().
late String result = _getResult(); // Lazily initialized.
In the example above , If you've never used variables , Will never call more expensive _getResult() function .
hypothesis _getResult() Is a very important function to calculate the result . however , If we assign it to any variable without delaying , that _getResult() Even if we don't use it , Every time I do .
No, late keyword
String result = _getResult();
In the code above ,result Never used , but _getResult() Will still be executed .
Use late keyword
late String result = _getResult();
In the code above _getResult() Not implemented , Because of the variable result Never used, found it , It's using late The modifier declares .
3、 You can delete many initState/constructor call !
before , If we want to create AnimationController, Must be in initState Constructor , because this, The required vsync Can only be accessed from methods .
AnimationController anim1;
@override
void initState() {
super.initState();
anim1 = AnimationController(vsync: this, duration: Duration(seconds: 1))..forward();
}
Now? , We can write this way :
late AnimationController anim = AnimationController(vsync: ``this``, duration: Duration(seconds: 1))..forward();
This saves you 6 Line code
You can use it to get the initial value from the calculation method , Or set any default value depending on the dynamic value .
You can also create builder methods , And call them :
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;
}
Unwanted initState, either @override Make every line meaningful .
Other summaries will be updated continuously !
END.
边栏推荐
- 叮!Techo Day 腾讯技术开放日如约而至!
- R语言ggplot2可视化:使用patchwork包(直接使用加号+)将一个ggplot2可视化结果和一个plot函数可视化结果横向组合起来形成最终结果图、将两个可视的组合结果对齐
- 买卖股票的最佳时机
- Validate palindrome string
- R语言ggplot2可视化:使用patchwork包(直接使用加号+)将一个ggplot2可视化结果和数据表格横向组合起来形成最终结果图
- Halcon basic summary (I) cutting pictures and rotating images
- 分布式理论须知
- go-zero 微服务实战系列(七、请求量这么高该如何优化)
- [C language] how to implement plural types
- Case driven: a detailed guide from getting started to mastering shell programming
猜你喜欢
![[C language] nextday problem](/img/7b/422792e07dd321e3a37c1fff55c0ca.png)
[C language] nextday problem

Classmate Zhang hasn't learned to be an anchor yet

信创操作系统--麒麟Kylin桌面操作系统 (项目十 安全中心)

看界面控件DevExpress WinForms如何创建一个虚拟键盘

Fleet | background Discovery issue 3: Status Management

SaaS application management platform solution in the education industry: help enterprises realize the integration of operation and management

Privacy computing fat - offline prediction
PostgreSQL实现按年、月、日、周、时、分、秒的分组统计

Go zero micro Service Practice Series (VII. How to optimize such a high demand)

Power battery is divided up like this
随机推荐
How can the digital intelligent supply chain management platform of the smart Park optimize process management and drive the development of the park to increase speed and quality?
Power battery is divided up like this
Do not use short circuit logic to write STL sorter multi condition comparison
云杉网络DeepFlow帮助5G核心网和电信云构建可观测性
[JS] Fibonacci sequence implementation (recursion and loop)
化学制品制造业智慧供应商管理系统深度挖掘供应商管理领域,提升供应链协同
Classmate Zhang hasn't learned to be an anchor yet
Fleet |「後臺探秘」第 3 期:狀態管理
Fleet | background Discovery issue 3: Status Management
开源大咖说 - Linus 与 Jim 对话中国开源
Talking about open source - Linus and Jim talk about open source in China
MIPS汇编语言学习-01-两数求和以及环境配置、如何运行
实验6 8255并行接口实验【微机原理】【实验】
雷科防务:4D毫米波雷达产品预计可以在年底量产供货
go-zero 微服务实战系列(七、请求量这么高该如何优化)
[C language] how to implement plural types
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语言ggplot2可视化:使用patchwork包(直接使用加号+)将一个ggplot2可视化结果和数据表格横向组合起来形成最终结果图
经典模型——Transformer