Boolean——Dart

简介: Dart中的布尔类型是 bool ,它只有两个字面值:true 和 false。都是编译时常量。与js不同,Dart支持类型安全,这意味着你不能使用 `if (nonbooleanValue)` 或者 `assert (nonbooleanValue)` 这种形式。

Boolean——Dart

Dart中的布尔类型是 bool ,它只有两个字面值:true 和 false。都是编译时常量。

与js不同,Dart支持类型安全,这意味着你不能使用 if (nonbooleanValue) 或者 assert (nonbooleanValue) 这种形式。

相反,你需要显式的检查这些值,如下:

// Check for an empty string.
var fullName = '';
assert(fullName.isEmpty);

// Check for zero.
var hitPoints = 0;
assert(hitPoints <= 0);

// Check for null.
var unicorn;
assert(unicorn == null);

// Check for NaN.
var iMeantToDoThis = 0 / 0;
assert(iMeantToDoThis.isNaN);

参考:
https://www.dartlang.org/guides/language/language-tour#booleans

相关文章
|
9月前
|
存储 安全 API
C++ 17 新特性 C++ String View:了解C++ 17 std::string_view的使用场景
C++ 17 新特性 C++ String View:了解C++ 17 std::string_view的使用场景
378 2
|
8月前
|
存储 Dart
Dart的Map类型
Dart的Map类型
100 0
|
6月前
|
Dart JavaScript 前端开发
Dart或Flutter中解决异常-type ‘int‘ is not a subtype of type ‘double‘
Dart或Flutter中解决异常-type ‘int‘ is not a subtype of type ‘double‘
208 4
|
6月前
|
Dragonfly Dart NoSQL
Dart ffi 使用问题之在Dart中调用String的toNativeUtf8方法时有什么是需要注意的
Dart ffi 使用问题之在Dart中调用String的toNativeUtf8方法时有什么是需要注意的
|
9月前
|
Dart JavaScript 安全
C++17新特性之std::string_view
std::string_view系C++17标准发布后新增的内容,类成员变量包含两个部分:字符串指针和字符串长度,相比std::string, std::string_view涵盖了std::string的所有只读接口。如果生成的std::string无需进行修改操作,可以把std::string转换为std::string_view,std::string_view记录了对应的字符串指针和偏移位置,无需管理内存,相对std::string拥有一份字符串拷贝,如字符串查找和拷贝,效率更高。
210 0
C++17新特性之std::string_view
|
Java
Boolean源码解剖学
Boolean源码解析
65 0
Dart报The return type ‘bool‘ isn‘t a ‘void‘, as required by the closure‘s context
Dart报The return type ‘bool‘ isn‘t a ‘void‘, as required by the closure‘s context
Flutter的setState的使用注意事项以及报错The method ‘setState‘ isn‘t defined for the type
Flutter的setState的使用注意事项以及报错The method ‘setState‘ isn‘t defined for the type
|
Dart 索引
[Flutter]足够入门的Dart语言系列之变量的类型:bool、String、num、List、Set和Map
变量的类型指的是变量的特性或特征,比如表示数字类型、文本类型、集合类型等,表示的是一类数据。 Dart提供以下类型:int, double、String、List、Set、Map、null...
743 0
[Flutter]足够入门的Dart语言系列之变量的类型:bool、String、num、List、Set和Map