我有 100 个通过蓝牙以 Uint8List 形式连续来自微控制器的数据样本。数据来自蓝牙缓冲区,我正在使用颤振蓝牙串行包通过蓝牙接收数据。数据以这种形式到达:
[115, 43, 48, 46, 56, 54, 101, 0, 0, 115]
[43, 49, 46, 55, 49, 101, 0, 0, 115, 43]
[0, 0, 115, 43, 51, 46, 53, 57, 101, 0, 0, 115, 43, 51, 46] ... ect
(note that this is just an example of the result and the completed result is about 80 lists of Uint all coming to the same var which is data)
包含 Uint 的 List 的大小并不一致。所以我想将所有这些即将到来的数据合并到一个列表中。
这是我尝试使用的代码,由于数据未合并,因此无法正常工作。
connection!.input!.listen(_onDataReceived).onDone(() {
if (isDisconnecting) {
print('Disconnecting locally!');
} else {
print('Disconnected remotely!');
}
if (this.mounted) {
setState(() {});
}
}
Future<void> _onDataReceived( Uint8List data ) async {
List newLst =[];
newLst.add(data);
int i = 0;
Queue stack = new Queue();
stack.addAll(newLst);
print(data);
print(stack);
}
如何将所有即将到来的数据组合在一个大列表中并将其存储以供以后操作。
请注意,这只是结果的一个示例,完成的结果是大约 80 个 Uint 列表,它们都来自同一个 var,即数据
如果我没记错的话,connection!.input
是一个Stream
。
如果是这样,那么您的问题似乎与您所说的列表无关,而与Stream
s.
由于您正在收听Stream
:
connection!.input!.listen(_onDataReceived).onDone(...);
如果您想要更改、修改或处理即将到来的数据,Stream
那么您可能想要使用一些本机Stream
API。
在这种特殊情况下,您希望减少所有即将发生的事件(列表Uint8List
)并处理为单个输出(Uint8List
包含所有数据的单个输出),然后您可以使用Stream<T>.reduce
which 返回 aFuture<T>
在没有更多即将发生的事件时解析(Stream
确实发出完成事件)。
您甚至不需要听,因为您只想处理单个输出而不是单独处理每个事件:
final Uint8List mergedData = await connection!.input!.reduce((previous, current) => Uint8List.fromList([...previous, ...current]));
查看这个 DartPad 代码片段以查看完整示例。
要查看完整的 API,请查看 s 的官方Dart 文档Stream
,它是完整且具有指导意义的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句