编解码转换相关
Uri
使用 encodeFull() 、decodeFull() 方法,对URI中除了特殊字符(例如 /、:、&、 #)以外的字符进行编解码
var uri = 'http://example.org/api?foo=some message';// 编码var encode = Uri.encodeFull(uri);print(encode); //http://example.org/api?foo=some%20message// 解码print(Uri.decodeFull(encode) == uri); //true
使用 encodeComponent() 、decodeComponent() 方法,对 URI 中具有特殊含义的所有字符串,
特殊字符包括(但不限于)/ 、& 、:
var uri = 'http://example.org/api?foo=some message';// 编码var encode = Uri.encodeComponent(uri);print(encode); //http%3A%2F%2Fexample.org%2Fapi%3Ffoo%3Dsome%20message// 解码print(Uri.decodeComponent(encode) == uri); //true
解析URI
var uri = 'http://example.org:8000/foo/bar#frag?foo=some&age=18';var u = Uri.parse(uri);print(u.scheme); //"http"print(u.host); //"example.org"print(u.path); //"/foo/bar"print(u.fragment); //"frag?foo=some&age=18"print(u.origin); //"http://example.org:8000"var uri2 = Uri(scheme: 'http',host: 'example.org',path: '/foo/bar',fragment: 'frag',);print(uri2.toString()); //http://example.org/foo/bar#frag
JSON
import "dart:convert";void main() {var j1 = '''[{"name": "张三", "age": 18},{"name": "李四", "age": 20}]''';// JSON字符串 转换为 Map对象var s1 = jsonDecode(j1);var s2 = json.decode(j1);print(s1); //[{name: 张三, age: 18}, {name: 李四, age: 20}]print(s2); //[{name: 张三, age: 18}, {name: 李四, age: 20}]// Map对象 转换为 JSON字符串print(jsonEncode(s1)); //[{"name":"张三","age":18},{"name":"李四","age":20}]print(json.encode(s1)); //[{"name":"张三","age":18},{"name":"李四","age":20}]}
只有 int, double, String, bool, null, List, 或者 Map 类型对象可以直接编码成 JSON。 List 和 Map 对象进行递归编码。
不能直接编码的对象有两种方式对其编码。 第一种方式是调用 encode() 时赋值第二个参数, 这个参数是一个函数, 该函数返回一个能够直接编码的对象 第二种方式是省略第二个参数,着这种情况下编码器调用对象的 toJson() 方法。 更多示例及 JSON 包相关链接,参考 JSON Support 。
编解码UTF-8字符
List<int> utf8Bytes = [0xc3, 0x8e, 0xc3, 0xb1, 0xc5, 0xa3, 0xc3, 0xa9,0x72, 0xc3, 0xb1, 0xc3, 0xa5, 0xc5, 0xa3, 0xc3,0xae, 0xc3, 0xb6, 0xc3, 0xb1, 0xc3, 0xa5, 0xc4,0xbc, 0xc3, 0xae, 0xc5, 0xbe, 0xc3, 0xa5, 0xc5,0xa3, 0xc3, 0xae, 0xe1, 0xbb, 0x9d, 0xc3, 0xb1];// 使用 utf8.decode() 解码 UTF8 编码的字符创为 Dart 字符创:var funnyWord = utf8.decode(utf8Bytes);var s2 = 'Îñţérñåţîöñåļîžåţîờñ';print(funnyWord == s2); //true// 使用 utf8.encode() 将 Dart 字符串编码为一个 UTF8 编码的字节流:print(utf8.encode(s2)); // [195, 142, 195, 177, ... , 157, 195, 177]
将 UTF-8 字符串流转换为 Dart 字符串,为 Stream 的 transform() 方法上指定 utf8.decoder:
var lines = inputStream.transform(utf8.decoder).transform(LineSplitter());try {await for (var line in lines) {print('Got ${line.length} characters from stream');}print('file is now closed');} catch (e) {print(e);}
定时器
import 'dart:async';void main() {// 回调1次Timer(Duration(milliseconds: 1000), () {print('end: ${DateTime.now()}');});// start: 2020-12-03 19:25:39.619022// end: 2020-12-03 19:25:40.627231// 回调多次int num = 0;print('num -- ${num}: ${DateTime.now()}');Timer.periodic(Duration(milliseconds: 1000), (t) {num++;print('num -- ${num}: ${DateTime.now()}');if (num >= 5) {t.cancel(); // 取消定时器t = null;}});// num -- 0: 2020-12-03 19:26:08.109163// num -- 1: 2020-12-03 19:26:09.113760// num -- 2: 2020-12-03 19:26:10.110394// num -- 3: 2020-12-03 19:26:11.110795// num -- 4: 2020-12-03 19:26:12.110285// num -- 5: 2020-12-03 19:26:13.110871}
其他功能
dart:convert 库同样包含 ASCII 和 ISO-8859-1 (Latin1) 转换器。 更多详情,参考 API docs for the dart:convert library。
