简体   繁体   English

将列表合并到一个列表中 flutter dart

[英]combine lists in one list in flutter dart

I have 100 sample of data coming from microcontroller via bluetooth in a form of Uint8List continuously.我有 100 个数据样本通过蓝牙以 Uint8List 的形式连续来自微控制器。 The data is coming through bluetooth buffer and I am using flutter bluetooth serial package to receive data via bluetooth.数据来自蓝牙缓冲区,我正在使用 flutter 蓝牙序列号 package 通过蓝牙接收数据。 The data is arriving in this form:数据以这种形式到达:

[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)

The size of the List containing the Uint is not consistent.包含 Uint 的 List 的大小不一致。 So I want to combine all these coming data in one List.所以我想将所有这些即将到来的数据合并到一个列表中。

This is the code I am trying to use which is not working as the data is not being combined.这是我尝试使用的代码,由于数据未合并,因此无法正常工作。


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);



  }

How can I combine all the coming data in one big list and store it for later operations.我如何将所有即将到来的数据合并到一个大列表中并将其存储以供以后操作。

Try below code试试下面的代码

void main() {
  List l1 = [115, 43, 48, 46, 56, 54, 101, 0, 0, 115];
  List l2 = [43, 49, 46, 55, 49, 101, 0, 0, 115, 43];
  List l3 = [0, 0, 115, 43, 51, 46, 53, 57, 101, 0, 0, 115, 43, 51, 46];

  l1.addAll(l2 + l3);
  print(l1);
}

Output:输出:

[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]

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请注意,这只是结果的一个示例,完成的结果是大约 80 个 Uint 列表,它们都来自同一个 var,即 data

If I'm not wrong , connection!.input is a Stream .如果我没记错的话, connection!.input是一个Stream

If so, then seems your problem isn't about lists as you said, but about Stream s.如果是这样,那么您的问题似乎不在于您所说的列表,而在于Stream s。

Since you are listening to a Stream :由于您正在收听Stream

connection!.input!.listen(_onDataReceived).onDone(...);

And you want to alter, modify or process the upcoming data from a Stream then you probably want to use some of the native Stream API.如果您想要更改、修改或处理来自Stream的即将到来的数据,那么您可能想要使用一些本机Stream API。

In this particular case, you want to reduce all upcoming events (list of Uint8List ) and process into a single output (a single Uint8List containing all data), then you can use Stream<T>.reduce which returns a Future<T> that resolves when there are no more upcoming events ( Stream did emit the done event).在这种特殊情况下,您希望减少所有即将发生的事件( Uint8List列表)并处理为单个输出(包含所有数据的单个Uint8List ),然后您可以使用Stream<T>.reduce返回一个Future<T>当没有更多即将发生的事件时解决( Stream确实发出了done事件)。

You don't even need to listen since you want just to process the single output not each event individually:您甚至不需要听,因为您只想处理单个输出而不是单独处理每个事件:

final Uint8List mergedData = await connection!.input!.reduce((previous, current) => Uint8List.fromList([...previous, ...current]));

Take a look at this DartPad snippet to see a full example.看看这个 DartPad 片段以查看完整示例。

To see full API, check out the official Dart documentation for Stream s, it's complete and didactic.要查看完整的 API,请查看Stream官方 Dart 文档,它是完整且具有指导意义的。

Check BytesBuilder:检查 BytesBuilder:

  var combinedList = BytesBuilder();
  var l1 = Uint8List(5);
  var l2 = Uint8List(5);
  combinedList.add(l1);
  combinedList.add(l2);
  var combinedList_to_Uint8List = combinedList.toBytes()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM