1

I have 100 sample of data coming from microcontroller via bluetooth in a form of Uint8List continuously. The data is coming through bluetooth buffer and I am using flutter bluetooth serial package to receive data via bluetooth. 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. 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.

10

4 Answers 4

1

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

If I'm not wrong, connection!.input is a Stream.

If so, then seems your problem isn't about lists as you said, but about Streams.

Since you are listening to a 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.

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

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.

To see full API, check out the official Dart documentation for Streams, it's complete and didactic.

0
0

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]
1
  • I think the question was not very clear please read the updated question in a min Jun 3, 2022 at 8:55
0

Check BytesBuilder:

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

I hope that your data will not overflow your memory when stored all in one. If OK, look:

List<T> concatLists<T>(List<List<T>> listOfLists, [bool isGrowable = true]) {
  if (listOfLists.first.isEmpty) {
    throw Exception('first list of lists cannot be empty');
  }

  int totalLength = 0;
  for (final list in listOfLists) {
    totalLength += list.length;
  }

  List<T> result =
      List<T>.filled(totalLength, listOfLists[0][0], growable: isGrowable);
  int idx = 0;
  for (final list in listOfLists) {
    for (final item in list) {
      result[idx] = item;
      idx += 1;
    }
      // result.addAll(list); // at least 2x longer
  }

  return result;
}

This is the most performant way I know. Just store this function somewhere inside your metaphorical "utils_and_helpers" folder and utilize when needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.