I am experimenting with Flutter and need to make a plugin package for Android and iOS, and have started with Android. The Android Java code I need to communicate with uses a byte array (byte[]) both as input and as return type for some of its methods. What does this map to in Dart?
A byte in Java is 8 bits. It is a primitive data type, meaning it comes packaged with Java. Bytes can hold values from -128 to 127. No special tasks are needed to use it; simply declare a byte variable and you are off to the races.
The easiest approach would be to create a byte list ( Uint8list ), then view the underlying buffer as a 32-bit integer list ( Int32List ) and store the integer there. That will allow you to read back the bytes.
Uint8List class Null safety. A fixed-length list of 8-bit unsigned integers. For long lists, this implementation can be considerably more space- and time-efficient than the default List implementation.
json now lets you put a byte[] object directly into a json and it remains readable. you can even send the resulting object over a websocket and it will be readable on the other side.
Here is the standard type mapping for platform channels:
https://flutter.io/platform-channels/#codec
On Android, byte[]
maps to Uint8List
.
Dart has a dart:typed_data
core library exactly for this purpose:
https://api.dartlang.org/stable/1.24.3/dart-typed_data/dart-typed_data-library.html
I'm not 100% sure of how this maps to the Flutter plugin model, though I suspect a Flutter user or developer can fill us in :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With