int
、double
、num
int
整型,取值通常在 -253 ~ 253 之间
int class
double
64-bt
(双精度)浮点数,符合 IEEE 754
标准。
double class
num
数值类型的基类
,int 和 double 都继承自num。
num class
// String -> int
int a = int.parse('123');
// String -> double
double b = double.parse('1.234');
// int -> String
String a = 123.toString();
// double -> String
String b = 1.234.toString();
print([a, b]);
// double -> int
double a = 1.23;
int b = a.toInt();
print(b);
bool
为了代表布尔值,Dart
有一个名字为 bool
的类型。只有两个对象是布尔类型:true
和 false
所创还能得对象,这两个对象也是编译时常量。
bool a;
print(a);
true
判断只有 true
对象才被认为是 true
。所有其他的值都是 false
。
String name = '123';
if (name) {
print('this is name'); // 不执行
}
assert
断言var a = true;
assert(a);
var name = '';
assert(name.isEmpty); // 是否为空
assert(name.isNotEmpty); // 是否不为空
var num = 0 / 0;
assert(num.isNaN);
Debug
模式下运行有效,如果在 Release
模式下运行,则断言不会执行。String
final myString = 'Bob\'s dog'; // Bob's dog
final myString = "a \"quoted\" word"; // a "quoted" word
final myString = "Bob's dog"; // Bob's dog
final myString = 'a "quoted" word'; // a "quoted" word
final value = '"quoted"'; // "quoted"
final myString = "a $value word"; // a "quoted" word
String
对象。$
或 ${}
引入某个对象进行字符串转义。var a = 'hello' + ' ' + 'word';
var a = 'hello'' ''word';
var a = 'hello' ' ' 'word';
var a = 'hello'
' '
'word';
var a = '''
hello word
this is multi line
''';
var a = """
hello word
this is multi line
""";
print(a);
var a = 'hello word \n this is multi line';
print(a);
hello word
this is multi line
在字符串内部使用 \
即为转义
var a = r'hello word \n this is multi line';
print(a);
hello word \n this is multi line
取消转义即在字符串前添加 r
修饰
var a = 'web site ducafecat.tech';
print(a.contains('ducafecat')); // 是否包含该字符串
print(a.startsWith('web')); // 是否以 web 开头
print(a.endsWith('tech')); // 是否以 tech 结尾
print(a.indexOf('site')); // site 在字符串中的位置
true
true
true
4
StringBuffer
使用var sb = StringBuffer();
sb..write('hello word!')
..write('my')
..write(' ')
..writeAll(['web', 'site', 'https://ducafecat.tech']);
print(sb.toString());
hello word!my websitehttps://ducafecat.tech
StringBuffer
是存储 String
的桶子,必需使用 toString()
函数才能得到 String
类型
DateTime
时间var now = new DateTime.now();
print(now);
2023-05-28 14:37:43.607
var date = new DateTime(2008, 10, 01, 9, 30);
print(date);
2008-10-01 09:30:00.000
var d = new DateTime.utc(2008, 10, 01, 9, 30);
print(d);
2008-10-01 09:30:00.000Z
var d = DateTime.parse('2008-10-01 09:30:30Z');
print(d);
2008-10-01 09:30:30.000Z
var d = DateTime.parse('2008-10-01 09:30:30+0800');
print(d);
2008-10-01 01:30:30.000Z
var d = DateTime.now();
print(d);
print(d.add(new Duration(minutes: 5)));
print(d.add(new Duration(minutes: -5)));
2023-10-01 22:09:12.805
2023-10-01 22:14:12.805
2023-10-01 22:04:12.805
var d1 = new DateTime(2008, 10, 1);
var d2 = new DateTime(2008, 10, 10);
print(d1.isAfter(d2)); // d1 是否在d2之后
print(d1.isBefore(d2)); // d1 是否在 d2 之前
false
true
var d1 = new DateTime.now()
var d2 = d1.add(new Duration(milliseconds: 30));
print(d1.isAtSameMomentAs(d2));
false
var d1 = new DateTime(2008, 10, 1);
var d2 = new DateTime(2008, 10, 10);
var difference = d1.difference(d2);
print([difference.inDays, difference.inHours]);
[-9, -216]
var now = new DateTime.now();
print(now.millisecondsSinceEpoch); // 秒级
print(now.microsecondsSinceEpoch); // 毫秒级