Flutter ☞ 数据类型

数值类型 intdoublenum

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 的类型。只有两个对象是布尔类型:truefalse 所创还能得对象,这两个对象也是编译时常量。

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 对象。
  • 在单引号内部如果使用单引号则需要转义,而使用双引号则不需要转义。反之亦然。
  • 在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
  • 协调世界时 UTC
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); // 毫秒级

你可能感兴趣的:(Flutter,flutter,c#,开发语言)