Android开发中Dart语言7个很酷的特点实用指南
平时做技术实践时,很多问题不是概念不会,而是细节没串起来。拿“Android开发中Dart语言7个很酷的特点”来说,它看着像小点,放到项目里常会牵出环境、配置、兼容性和维护成本。下面按实际采用顺序,把思路、关键写法和容易踩坑的地方讲清楚,便于大家直接对照操作。
目录
- 参考
- 正文
- Cascade 级联
- Abstract 抽象类
- Factory constructors 工厂建造者
- Named 命名构造函数
- Mixins 混合物
- Typedefs
- Extension 扩展方法
- unawaited_futures
参考
(链接已移除)
正文
从实现思路看,今天的文章简短地揭示了 Dart 语言所提供的很酷的特性。更多时候,这些选项对于轻松的应用程序是不必要的,但是当你想要借助轻松、清晰和简洁来改进你的代码时,这些选项是一个救命稻草。
考虑到这一点,我们走吧。
Cascade 级联
结合项目来看,Cascades (.., ?..) 允许你对同一个对象进行一系列操作。这通常节省了新建临时变量的步骤,同时允许您编写更多流畅的代码。
var paint = Paint();
paint.color = Colors.black;
paint.strokeCap = StrokeCap.round;
paint.strokeWidth = 5.0;
//above block of code when optimized
var paint = Paint()
..color = Colors.black
..strokeCap = StrokeCap.round
..strokeWidth = 5.0;
Abstract 抽象类
理解这一步时,采用 abstract 修饰符定义一个 _abstract 抽象类(无法实例化的类)。抽象类对于定义接口很有用,通常带有一些实现。
// This class is declared abstract and thus
// can't be instantiated.
abstract class AbstractContainer {
// Define constructors, fields, methods...
void updateChildren(); // Abstract method.
}
Factory constructors 工厂建造者
在实现不总是新建类的新实例的构造函数时采用 factory 关键字。
class Logger {
String name;
Logger(this.name);
factory Logger.fromJson(Map<String, Object> json) {
return Logger(json['name'].toString());
}
}
Named 命名构造函数
采用命名构造函数为一个类实现多个构造函数或者提供额外的清晰度:
class Points {
final double x;
final double y;
//unnamed constructor
Points(this.x, this.y);
// Named constructor
Points.origin(double x,double y)
: x = x,
y = y;
// Named constructor
Points.destination(double x,double y)
: x = x,
y = y;
}
Mixins 混合物
Mixin 是在多个类层次结构中重用类代码的一种方法。
理解这一步时,要实现 implement mixin,新建一个声明没有构造函数的类。除非您希望 mixin 能够作为常规类采用,否则请采用 mixin 关键字而不是类。
在这个场景下,若要采用 mixin,请采用后跟一个或多个 mixin 名称的 with 关键字。
落到代码里,若要限制能够采用 mixin 的类型,请采用 on 关键字指定所需的超类。
class Musician {}
//creating a mixin
mixin Feedback {
void boo() {
print('boooing');
}
void clap() {
print('clapping');
}
}
//only classes that extend or implement the Musician class
//can use the mixin Song
mixin Song on Musician {
void play() {
print('-------playing------');
}
void stop() {
print('....stopping.....');
}
}
//To use a mixin, use the with keyword followed by one or more mixin names
class PerformSong extends Musician with Feedback, Song {
//Because PerformSong extends Musician,
//PerformSong can mix in Song
void awesomeSong() {
play();
clap();
}
void badSong() {
play();
boo();
}
}
void main() {
PerformSong().awesomeSong();
PerformSong().stop();
PerformSong().badSong();
}
Typedefs
在这个场景下,类型别名ー是指代类型的一种简明方式。通常用来新建在项目里经常采用的自定义类型。
typedef IntList = List<int>;
List<int> i1=[1,2,3]; // normal way.
IntList i2 = [1, 2, 3]; // Same thing but shorter and clearer.
//type alias can have type parameters
typedef ListMapper<X> = Map<X, List<X>>;
Map<String, List<String>> m1 = {}; // normal way.
ListMapper<String> m2 = {}; // Same thing but shorter and clearer.
Extension 扩展方法
落到代码里,在 Dart 2.7 中引入的扩展方法是一种向现有库和代码中添加功能的方法。
//extension to convert a string to a number
extension NumberParsing on String {
int customParseInt() {
return int.parse(this);
}
double customParseDouble() {
return double.parse(this);
}
}
void main() {
//various ways to use the extension
var d = '21'.customParseDouble();
print(d);
var i = NumberParsing('20').customParseInt();
print(i);
}
可选的位置参数
落到代码里,借助将位置参数包装在方括号中,能够使位置参数成为可选参数。可选的位置参数在函数的参数列表中总是最后一个。除非您提供另一个默认值,否则它们的默认值为 null。
String joinWithCommas(int a, [int? b, int? c, int? d, int e = 100]) {
var total = '$a';
if (b != null) total = '$total,$b';
if (c != null) total = '$total,$c';
if (d != null) total = '$total,$d';
total = '$total,$e';
return total;
}
void main() {
var result = joinWithCommas(1, 2);
print(result);
}
unawaited_futures
实际处理时,您想要启动一个 Future 时,建议的方法是采用 unawaited
否则你不加 async 就不会执行了
import 'dart:async';
Future doSomething() {
return Future.delayed(Duration(seconds: 5));
}
void main() async {
//the function is fired and awaited till completion
await doSomething();
// Explicitly-ignored
//The function is fired and forgotten
unawaited(doSomething());
}
在这个场景下,以上就是Android开发Dart语言7个很酷的特点的详细内容,更多关于Android开发Dart特点的资料请关注脚本之家其它相关文章!
您可能感兴趣的文章:
- Dart语法之变量声明与数据类型实例详解
- Google Dart编程语法和基本类型学习教程
- Flutter入门学习Dart语言变量及基本采用概念
- Android开发Dart Constructors构造函数采用技巧整理
- Dart 异步编程生成器及自定义类型用法详解
-
09.02
荣耀机器人闪电 400 米跑出 40.6 秒成绩,打破该项人类世界纪录
-
09.02
成都车展MG 07上市,续航智驾双越级,10.59万起或成新能源轿跑新宠
-
09.02
复古数码相机镜面自拍
-
09.02
漫画书海报风格
-
09.02
简单的Lua 连接操作mysql数据库的做法实用指南
-
09.02
DeepSeek+Power-主要信息和内容重点
-
-
- 关于Dart中的异步编程实用指南
- 09.02
-
- 掌握高效汇总Excel表格数据的四种实用实用技巧
- 09.02
-
-
-
- 如何高效将多个表格数据汇总到一个表格中?
- 09.02
-
-
下载
- |
-
-
下载
- 《行尸走肉第一章》免安装中文汉化硬盘版下载
- 单机|436 MB
- 一款以动作冒险为主题的游戏
-
-
下载
- 《街头霸王X铁拳》免安装中文汉化硬盘版下载
- 单机|111MB
- 一款非常好玩的格斗游戏
-
-
下载
- |
-
-
下载
- 《暗黑破坏神3》免安装繁体中文正式版下载
- 单机|7630 MB
- 一款以角色扮演为主题的游戏
-
-
下载
- 《马克思佩恩3》免安装硬盘版下载
- 单机|27033 MB
- 一款以第三人称射击为主题的游戏