美文网首页
Dart 一些高效用法实例

Dart 一些高效用法实例

作者: NightRainBreeze | 来源:发表于2020-01-09 13:39 被阅读0次

空处理

?? ?. 的使用

如果你还不清楚?? ?.的意思=> Dart 如何优雅的避空

  • 推荐使用 ??null转换为需要的值
Text(title ?? '')

if(title?.isEmpty ?? true){
  // ...
}
  • 不推荐使用==赋值
// 不推荐
if(title?.isEmpty == true){
  // ...
}
  • 解析json实体数据null的处理
// list为null的处理赋值空list
moreList = List<SRItemModel>.from(map["moreList"]?.map((it) => SRItemModel.fromJsonMap(it)) ?? [])
// 字段为null的处理
goodsName = map["goodsName"] ?? '',
specName = map["specName"] ?? '',
count = map["goodsQty"] ?? 0,
list = map["list"] ?? [],

字符串相关

使用临近字符字的方式连接字面量字符串
不需要使用 + 来连接它们。应该像 CC++ 一样,只需要将它们挨着在一起就可以了。这种方式非常适合不能放到一行的长字符串的创建:

raiseAlarm(
    'ERROR: Parts of the spaceship are on fire. Other '
    'parts are overrun by martians. Unclear which are which.');
  • 而不是类似于Java使用+连接
// 不推荐
raiseAlarm('ERROR: Parts of the spaceship are on fire. Other ' +
    'parts are overrun by martians. Unclear which are which.');
  • 使用$插值的形式来组合字符串和值
'Hello, $name! You are ${year - teacher.birth} years old.';

// 不要使用不必要的大括号
'Hello, ${name}';

// 更不要再使用这种方式了, 使用$会看起来更连贯
'Hello, ' + name + '! You are ' + (year - birth).toString() + ' y...';

集合相关

Dart高效之操作集合

首先来看一下dart创建集合的推荐姿势:

  • 集合字面量
var points = []; // var points = List();
var addresses = {}; // var addresses = Map();
  • 指定类型
var points = <Point>[]; // var points = List<Point>();
var addresses = <String, Address>{}; // var addresses = Map<String, Address>();
List<int> singletonList(int value) {
  var list = <int>[]; // List<int> list = [];
  list.add(value);
  return list;
}
  • 使用.isEmpty.isNotEmpty替代.length
if (nameList.isEmpty)  // nameList.length == 0

// 使用 ?? 替代 ==
if (nameList?.isEmpty ?? true)  // nameList == null || nameList.length == 0

隐式newconst

  • new 关键字成为可选项
Widget build(BuildContext context) {
  return Row(
    children: [
      RaisedButton(
        child: Text('Increment'),
      ),
      Text('Click!'),
    ],
  );
}
  • 弃用和删除 new
Widget build(BuildContext context) {
  return new Row(
    children: [
      new RaisedButton(
        child: new Text('Increment'),
      ),
      new Text('Click!'),
    ],
  );
}
  • primaryColors 是const, 它的内容const关键字是隐式的,不需要写:
const primaryColors = [
  Color("red", [255, 0, 0]),
  Color("green", [0, 255, 0]),
  Color("blue", [0, 0, 255]),
];
const primaryColors = const [
  const Color("red", const [255, 0, 0]),
  const Color("green", const [0, 255, 0]),
  const Color("blue", const [0, 0, 255]),
];

=>箭头语法

=>这种箭头语法是一种定义函数的方法,该函数将在其右侧执行表达式并返回其值

  • getset
class Circle {
  num radius;
  int _width;

  Circle(this.radius, this._width);

  num get area => pi * radius * radius;
  num get circumference => pi * 2.0 * radius;
  set width(int width) => _width = width;
}
bool hasEmpty = aListOfStrings.any((s) {
  return s.isEmpty;
});
bool hasEmpty = aListOfStrings.any((s) => s.isEmpty);

级连..

要对同一对象执行一系列操作,请使用级联..

var button = querySelector('#confirm');
button.text = 'Confirm';
button.classes.add('important');
button.onClick.listen((e) => window.alert('Confirmed!'));
// 级连
querySelector('#confirm')
..text = 'Confirm'
..classes.add('important')
..onClick.listen((e) => window.alert('Confirmed!'));

相关文章

  • Dart 一些高效用法实例

    空处理 ?? ?. 的使用 如果你还不清楚?? ?.的意思=> Dart 如何优雅的避空 推荐使用 ?? 将nul...

  • Flutter中Dart基础

    Flutter是基于Dart语音开发的,Dart是面向对象的语言,下面记录一些Dart语言常用数据类型的用法。 1...

  • Flutter 知识梳理 (Dart) - Dart 和 Jav

    在学习Dart的时候,会遇到一些Java中没有的概念或者用法,这篇文章总结了Dart和Java中一些不同,但又经常...

  • Dart record

    参考 Dart学习笔记(29):异步编程Dart编程字典子不语归来 的 Dart2基础何小有Dart实例教程 数组...

  • Asible常用模块

    主机连通测试 command模块 模块中常见的一些用法 用法实例: 3、shell 模块 shell模块可以在远程...

  • Retrofit接口实例

    一、接口实例 二、TODO 1、Part、 PartMap、MultilPart用法实例2、Streaming用法...

  • Dart语法学习1

    前言 为什么学习Dart? Dart语法简洁高效,拥有数以千计的packages 生态系统 Dart 提供提前编...

  • Linux crontab 详细

    一、用法 二、实例

  • extends 、 implements 、 with的用法与区

    Flutter Dart语法(1):extends 、 implements 、 with的用法与区别 在Flut...

  • From JavaScript To Dart

    From JavaScript To Dart Dart 的一些特性 Dart 是静态类型的,但是 Dart 可以...

网友评论

      本文标题:Dart 一些高效用法实例

      本文链接:https://www.haomeiwen.com/subject/bckyactx.html