如何在 Dart 中读取字节数组数据?

Dil*_*eep 4 dart flutter

连接TCP Socket服务器并发送Request。服务器也以字节数组形式发送响应。如何在 dart 中读取字节数组数据。

Socket.connect('localhost', 8081)
  .then((socket) {
//Establish the onData, and onDone callbacks
socket.listen((data) {
  print(new String.fromCharCodes(data).trim()); //Here data is byte[]
  //How to read byte array data

},
    onDone: () {
      print("Done");
      // socket.destroy();

    },
    onError: (e) {
      print('Server error: $e');
    });
 socket.add([255, 12, 0, 11, 0, 9, 34, 82, 69, 70, 84, 65, 72, 73, 76]);
 });
}
Run Code Online (Sandbox Code Playgroud)

Ily*_*lya 6

这取决于编码为字节的数据类型。让我们假设它String 然后你可以用图书馆来做到这一点dart:convert

import 'dart:convert' show utf8;

final decoded = utf8.decode(data);
Run Code Online (Sandbox Code Playgroud)