文章

bool类型常用属性与方法

bool类型常用属性与方法

bool类型常用属性与方法

bool类型常用属性与方法

Dart 布尔类型常用属性与方法详解

布尔类型属性文档

isNegative

🔍** isNegative → **bool

介绍: 判断布尔值是否为 false。这是一个自定义扩展属性,实际开发中可根据需要定义。
类似JS: 无直接对应
示例:

1
2
3
4
5
6
7
8
9
10
// 假设有此扩展属性
extension BoolExtensions on bool {
  bool get isNegative => !this;
}

bool value = false;
print(value.isNegative); // 输出: true

value = true;
print(value.isNegative); // 输出: false

JS示例:

1
2
3
4
5
6
// JavaScript 无直接对应
let value = false;
console.log(!value); // 输出: true

value = true;
console.log(!value); // 输出: false

isTrue

🔍** isTrue → **bool

介绍: 判断布尔值是否为 true。在 Dart 某些包中(如 ActiveBool)提供此属性。
类似JS: value === true
示例:

1
2
3
4
5
6
7
8
// 在 ActiveBool 中的实现
bool value = true;
// ActiveBool active = ActiveBool(value);
// print(active.isTrue); // 输出: true

// 普通使用
print(true.isTrue); // 输出: true
print(false.isTrue); // 输出: false

JS示例:

1
2
3
4
5
let value = true;
console.log(value === true); // 输出: true

value = false;
console.log(value === true); // 输出: false

isFalse

🔍** isFalse → **bool

介绍: 判断布尔值是否为 false。在 Dart 某些包中(如 ActiveBool)提供此属性。
类似JS: value === false
示例:

1
2
3
4
5
6
7
bool value = false;
// ActiveBool active = ActiveBool(value);
// print(active.isFalse); // 输出: true

// 普通使用
print(false.isFalse); // 输出: true
print(true.isFalse); // 输出: false

JS示例:

1
2
3
4
5
let value = false;
console.log(value === false); // 输出: true

value = true;
console.log(value === false); // 输出: false

hashCode

🔍** hashCode → **int

介绍: 返回布尔值的哈希码。true 和 false 有各自不同的哈希码。
类似JS: 无直接对应
示例:

1
2
print(true.hashCode); // 输出: 1231 (示例值,实际可能不同)
print(false.hashCode); // 输出: 1237 (示例值,实际可能不同)

JS示例:

1
2
// JavaScript 无直接对应
// 布尔值没有内置的 hashCode 属性

runtimeType

🔍** runtimeType → **Type

介绍: 返回对象的运行时类型。
类似JS: typeof 操作符
示例:

1
2
3
4
5
bool flag = true;
print(flag.runtimeType); // 输出: bool

flag = false;
print(flag.runtimeType); // 输出: bool

JS示例:

1
2
3
4
5
let flag = true;
console.log(typeof flag); // 输出: "boolean"

flag = false;
console.log(typeof flag); // 输出: "boolean"

布尔类型方法文档

toString

🔤** toString() → **String

介绍: 返回布尔值的字符串表示。true 返回 “true”,false 返回 “false”。
类似JS: Boolean.prototype.toString()
示例:

1
2
print(true.toString()); // 输出: "true"
print(false.toString()); // 输出: "false"

JS示例:

1
2
console.log(true.toString()); // 输出: "true"
console.log(false.toString()); // 输出: "false"

compareTo

📊** compareTo(bool other) → **int

介绍: 比较两个布尔值。Dart 中布尔值实现了 Comparable 接口。
类似JS: 无直接对应,需要手动实现
示例:

1
2
3
4
print(true.compareTo(true)); // 输出: 0
print(true.compareTo(false)); // 输出: 1
print(false.compareTo(true)); // 输出: -1
print(false.compareTo(false)); // 输出: 0

JS示例:

1
2
3
4
5
6
7
8
9
10
// JavaScript 需要手动实现比较
function compareBools(a, b) {
    if (a === b) return 0;
    return a ? 1 : -1;
}

console.log(compareBools(true, true)); // 输出: 0
console.log(compareBools(true, false)); // 输出: 1
console.log(compareBools(false, true)); // 输出: -1
console.log(compareBools(false, false)); // 输出: 0

noSuchMethod

** noSuchMethod(Invocation invocation) → **dynamic

介绍: 当访问不存在的方法或属性时调用。
类似JS: ProxynoSuchMethod 模拟
示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class CustomBool {
  final bool value;
  
  CustomBool(this.value);
  
  @override
  dynamic noSuchMethod(Invocation invocation) {
    print('方法 ${invocation.memberName} 不存在');
    return null;
  }
}

var custom = CustomBool(true);
// custom.nonExistentMethod(); // 会触发 noSuchMethod

JS示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
// JavaScript 使用 Proxy 模拟
let customBool = new Proxy({ value: true }, {
  get(target, property) {
    if (property in target) {
      return target[property];
    }
    console.log(`属性 ${property} 不存在`);
    return undefined;
  }
});

console.log(customBool.value); // 输出: true
console.log(customBool.nonExistent); // 输出: undefined 并打印消息

布尔类型操作符文档

operator ==

⚖️** operator ==(Object other) → **bool

介绍: 相等运算符,比较两个布尔值是否相等。
类似JS: === 严格相等运算符
示例:

1
2
3
4
print(true == true); // 输出: true
print(true == false); // 输出: false
print(false == false); // 输出: true
print(true == 1); // 输出: false (Dart 是类型安全的)

JS示例:

1
2
3
4
console.log(true === true); // 输出: true
console.log(true === false); // 输出: false
console.log(false === false); // 输出: true
console.log(true === 1); // 输出: false

operator &

🔗** operator &(bool other) → **bool

介绍: 逻辑与运算(”and”),两个操作数都为 true 时返回 true。
类似JS: && 逻辑与运算符
示例:

1
2
3
4
print(true & true); // 输出: true
print(true & false); // 输出: false
print(false & true); // 输出: false
print(false & false); // 输出: false

JS示例:

1
2
3
4
console.log(true && true); // 输出: true
console.log(true && false); // 输出: false
console.log(false && true); // 输出: false
console.log(false && false); // 输出: false

operator |

🔗** operator(bool other) → **bool

介绍: 逻辑或运算(”inclusive or”),任一操作数为 true 时返回 true。
类似JS: || 逻辑或运算符
示例:

1
2
3
4
print(true | true); // 输出: true
print(true | false); // 输出: true
print(false | true); // 输出: true
print(false | false); // 输出: false

JS示例:

1
2
3
4
console.log(true || true); // 输出: true
console.log(true || false); // 输出: true
console.log(false || true); // 输出: true
console.log(false || false); // 输出: false

operator ^

🔗** operator ^(bool other) → **bool

介绍: 逻辑异或运算(”exclusive or”),两个操作数不同时返回 true。
类似JS: 无直接对应,需要手动实现
示例:

1
2
3
4
print(true ^ true); // 输出: false
print(true ^ false); // 输出: true
print(false ^ true); // 输出: true
print(false ^ false); // 输出: false

JS示例:

1
2
3
4
5
6
7
8
9
// JavaScript 需要手动实现异或
function xor(a, b) {
    return (a || b) && !(a && b);
}

console.log(xor(true, true)); // 输出: false
console.log(xor(true, false)); // 输出: true
console.log(xor(false, true)); // 输出: true
console.log(xor(false, false)); // 输出: false

静态方法文档

bool.fromEnvironment

🏭** bool.fromEnvironment(String name, {bool defaultValue = false}) → **bool

介绍: 返回环境声明 name 的布尔值。
类似JS: process.env 在 Node.js 中
示例:

1
2
3
4
5
6
// 检查是否存在某个环境变量
bool isDebug = bool.fromEnvironment('DEBUG', defaultValue: false);
bool isProduction = bool.fromEnvironment('PRODUCTION', defaultValue: true);

print('Debug mode: $isDebug');
print('Production mode: $isProduction');

JS示例:

1
2
3
4
5
6
// Node.js 环境
const isDebug = process.env.DEBUG === 'true' || false;
const isProduction = process.env.PRODUCTION !== 'false';

console.log(`Debug mode: ${isDebug}`);
console.log(`Production mode: ${isProduction}`);

bool.hasEnvironment

🏭** bool.hasEnvironment(String name) → **bool

介绍: 检查是否存在指定名称的环境声明。
类似JS: process.env 检查在 Node.js 中
示例:

1
2
3
4
5
bool hasDebug = bool.hasEnvironment('DEBUG');
bool hasFeatureFlag = bool.hasEnvironment('FEATURE_FLAG');

print('Has DEBUG: $hasDebug');
print('Has FEATURE_FLAG: $hasFeatureFlag');

JS示例:

1
2
3
4
5
6
// Node.js 环境
const hasDebug = 'DEBUG' in process.env;
const hasFeatureFlag = 'FEATURE_FLAG' in process.env;

console.log(`Has DEBUG: ${hasDebug}`);
console.log(`Has FEATURE_FLAG: ${hasFeatureFlag}`);

补充说明

注意事项

  1. 类型安全: Dart 是类型安全的,不能使用 if (nonBooleanValue),必须显式检查值
  2. 只有两个值: Dart 布尔类型只有 truefalse 两个字面值
  3. 编译时常量: truefalse 都是编译时常量

常用开发场景

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 1. 条件检查
var fullName = '';
assert(fullName.isEmpty); // 检查空字符串

var hitPoints = 0;
assert(hitPoints <= 0); // 检查零值

var unicorn;
assert(unicorn == null); // 检查 null

var iMeantToDoThis = 0 / 0;
assert(iMeantToDoThis.isNaN); // 检查 NaN

// 2. 逻辑运算组合
bool canEdit = true;
bool isAdmin = false;
bool isOwner = true;

bool hasEditPermission = canEdit && (isAdmin || isOwner);
print(hasEditPermission); // 输出: true

// 3. 环境配置检查
bool isDevelopment = bool.fromEnvironment('DEV_MODE');
bool enableLogging = bool.hasEnvironment('ENABLE_LOGGING') 
    ? bool.fromEnvironment('ENABLE_LOGGING') 
    : true;

// 4. 布尔值转换
String userInput = "true";
bool isVerified = userInput.toLowerCase() == 'true';
int binaryValue = isVerified ? 1 : 0;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// JavaScript 对应示例
// 1. 条件检查
let fullName = '';
console.log(fullName.length === 0); // 检查空字符串

let hitPoints = 0;
console.log(hitPoints <= 0); // 检查零值

let unicorn;
console.log(unicorn === undefined || unicorn === null); // 检查 null 或 undefined

let iMeantToDoThis = 0 / 0;
console.log(isNaN(iMeantToDoThis)); // 检查 NaN

// 2. 逻辑运算组合
let canEdit = true;
let isAdmin = false;
let isOwner = true;

let hasEditPermission = canEdit && (isAdmin || isOwner);
console.log(hasEditPermission); // 输出: true

// 3. 环境配置检查 (Node.js)
let isDevelopment = process.env.NODE_ENV === 'development';
let enableLogging = process.env.ENABLE_LOGGING !== 'false';

// 4. 布尔值转换
let userInput = "true";
let isVerified = userInput.toLowerCase() === 'true';
let binaryValue = isVerified ? 1 : 0;
本文由作者按照 CC BY 4.0 进行授权