Skip to content

Conversation

@szakarias
Copy link
Contributor

No description provided.

Copy link
Collaborator

@isoos isoos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with renames

@szakarias szakarias merged commit aebf957 into dart-lang:master Oct 30, 2024
32 checks passed
for (int i = 0; i < dataPoints.length; i++) {
byteData.setUint32(4 * i, dataPoints[i], Endian.little);
}
return base64Encode(byteData.buffer.asUint8List());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just use the Uint32List constructor. Give it a length and use setAll

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@szakarias I'd suggest:

  return base64.encode(Uint8List.sublistView(Uint32List.fromList(dataPoints)));

Or

  return base64.encode(Uint32List.fromList(dataPoints).buffer.asUint8List());

But I'd advise against getting used to using .buffer.asUint8List(). Because when you have a Uint32List the underlying buffer could be longer than the contents of the actual Uint32List.

So technically, you'd have to read the documentation for the Uint32List constructor to see that it says:

The list is backed by a ByteBuffer containing precisely length times 4 bytes.

Relying on this promise it is safe to use .buffer.asUint8List().

If we had gotten the Uint32List from somewhere else, we'd have to read offsetInBytes and lengthInBytes and pass them when doing .buffer.asUint8List().

Hence, why I'd generally causing against accessing .buffer directly, because unless you know where the buffer was created, the location of your data within the buffer is not guaranteed. But Uint8List.sublistView will avoid such concerns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants