dart 学习 之 字符串插值,空变量 null,避空运算符,条件属性访问,集合字面量,箭头语法

文章目录

字符串插值(String interpolation)

下面是一些使用字符串插值的例子:

Here are some examples of using string interpolation:

复制代码
String     									result
字符串 										结果
'${3 + 2}'	 	  							 '5'
'${"word".toUpperCase()}'	 				'WORD'
The value of '$myObject' 					myObject.toString().

小练习:

Q: 下面的方法接收两个整型变量作为参数,然后让它返回一个包含以空格分隔的整数的字符串。例如,stringify(2, 3) 应该返回 '2 3'。

A:

复制代码
String stringify(int x, int y) {
  return '${x} ${y}'
}

或者

String stringify(int x, int y) {

return 'KaTeX parse error: Double superscript at position 10: {x}' + ' '̲ + '{y}';

}

空变量 null

Dart 要求使用健全的空安全,这意味着除非变量显式声明为可空类型,否则它们将不能为空。换句话说,类型默认是不可为空的。

举个例子,下面的代码在空安全下是有错误的,因为 int 类型的变量不能为 null:

复制代码
int a = null; // INVALID.

你可以通过在类型后添加 ? 来表示该类型可空:

复制代码
int? a = null; // Valid.

在所有 Dart 版本中,null 在未初始化的变量里都是默认值,所以你可以这样简化你的代码:

复制代码
int? a; // The initial value of a is null.

Q:

试着定义以下两种变量:

一个可空的 String,名为 name,值为 'Jane'。

一个可空的 String,名为 address,值为 null。

A:

复制代码
String ?name = 'Jane';
String ?address ;

避空运算符

Dart 提供了一系列方便的运算符用于处理可能会为空值的变量。其中一个是 ??= 赋值运算符,仅当该变量为空值时才为其赋值:

复制代码
int? a; // = null
a ??= 3;
print(a); // <-- Prints 3.

a ??= 5;
print(a); // <-- Still prints 3.

另外一个避空运算符是 ??,如果该运算符左边的表达式返回的是空值,则会计算并返回右边的表达式。

复制代码
print(1 ?? 3); // <-- Prints 1.
print(null ?? 12); // <-- Prints 12.
String? baz = foo ?? bar; // 如果 foo 是 null 才会赋值为 bar

条件属性访问

要保护可能会为空的属性的正常访问,请在点(.)之前加一个问号(?)。

复制代码
myObject?.someProperty

上述代码等效于以下内容:

复制代码
(myObject != null) ? myObject.someProperty : null

你可以在一个表达式中连续使用多个 ?.:

复制代码
myObject?.someProperty?.someMethod()

如果 myObject 或 myObject.someProperty 为空,则前面的代码返回 null(并不再调用 someMethod)。

Q:

尝试使用条件属性访问来完成下面的代码片段。

复制代码
// This method should return the uppercase version of `str`
// or null if `str` is null.
String? upperCaseIt(String? str) {
  // Try conditionally accessing the `toUpperCase` method here.
  
}

A:

复制代码
return str?.toUpperCase()  ;  // 如果不为空就返回大写字母

集合字面量

Dart 内置了对 list、map 以及 set 的支持。你可以通过字面量直接创建它们:

复制代码
final aListOfStrings = ['one', 'two', 'three'];
final aSetOfStrings = {'one', 'two', 'three'};
final aMapOfStringsToInts = {
  'one': 1,
  'two': 2,
  'three': 3,
};

Dart 的类型推断可以自动帮你分配这些变量的类型。在这个例子中,推断类型是 List、Set和 Map<String, int>。

你也可以手动指定类型:

复制代码
final aListOfInts = <int>[];
final aSetOfInts = <int>{};
final aMapOfIntToDouble = <int, double>{};

在使用子类型的内容初始化列表,但仍希望列表为 List 时,指定其类型很方便:

复制代码
final aListOfBaseType = <BaseType>[SubType(), SubType()];

Q:

尝试将以下变量设定为指定的值。替换当前的 null 值。

复制代码
// Assign this a list containing 'a', 'b', and 'c' in that order:
final aListOfStrings =null ;

// Assign this a set containing 3, 4, and 5:
final aSetOfInts = null;

// Assign this a map of String to int so that aMapOfStringsToInts['myKey'] returns 12:
final aMapOfStringsToInts = null;

// Assign this an empty List<double>:
final anEmptyListOfDouble = null;

// Assign this an empty Set<String>:
final anEmptySetOfString = null;

// Assign this an empty Map of double to int:
final anEmptyMapOfDoublesToInts = null;

A:

复制代码
// Assign this a list containing 'a', 'b', and 'c' in that order:
final aListOfStrings = ['a', 'b', 'c'];

// Assign this a set containing 3, 4, and 5:
final aSetOfInts = {3, 4, 5};

// Assign this a map of String to int so that aMapOfStringsToInts['myKey'] returns 12:
final aMapOfStringsToInts = {'myKey': 12};

// Assign this an empty List<double>:
final anEmptyListOfDouble = <double>[];

// Assign this an empty Set<String>:
final anEmptySetOfString = <String>{};

// Assign this an empty Map of double to int:
final anEmptyMapOfDoublesToInts = <double, int>{};

箭头语法

你也许已经在 Dart 代码中见到过 => 符号。这种箭头语法是一种定义函数的方法,该函数将在其右侧执行表达式并返回其值。

例如,考虑调用这个 List 类中的 any 方法:

复制代码
bool hasEmpty = aListOfStrings.any((s) {
  return s.isEmpty;
});

这里是一个更简单的代码实现:

复制代码
bool hasEmpty = aListOfStrings.any((s) => s.isEmpty);

看看下面的代码

复制代码
class MyClass {
  int value1 = 2;
  int value2 = 3;
  int value3 = 5;

  // Returns the product of the above values:
  int get product => value1 * value2 * value3;
  
  // Adds 1 to value1:
  void incrementValue1() => value1++; 
  
  // Returns a string containing each item in the
  // list, separated by commas (e.g. 'a,b,c'): 
  String joinWithCommas(List<String> strings) => strings.join(',');
}
相关推荐
晨非辰1 小时前
#C语言——刷题攻略:牛客编程入门训练(十一):攻克 循环控制(三),轻松拿捏!
c语言·开发语言·经验分享·学习·visual studio
xiaoxiaoxiaolll2 小时前
期刊速递 | 《Light Sci. Appl.》超宽带光热电机理研究,推动碳纳米管传感器在制药质控中的实际应用
人工智能·学习
励志码农3 小时前
JavaWeb 30 天入门:第二十三天 —— 监听器(Listener)
java·开发语言·spring boot·学习·servlet
天高云淡ylz3 小时前
子网掩码的隐形陷阱:为何能ping通却无法HTTPS访问
开发语言·php
DisonTangor3 小时前
字节开源 OneReward: 通过多任务人类偏好学习实现统一掩模引导的图像生成
学习·ai作画·开源·aigc
黎宇幻生4 小时前
Java全栈学习笔记33
java·笔记·学习
2501_926227945 小时前
.Net程序员就业现状以及学习路线图(五)
学习·.net
希望20175 小时前
Golang Panic & Throw & Map/Channel 并发笔记
开发语言·golang
朗迹 - 张伟5 小时前
Golang安装笔记
开发语言·笔记·golang
yzx9910135 小时前
生活在数字世界:一份人人都能看懂的网络安全生存指南
运维·开发语言·网络·人工智能·自动化