Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Dart equivalent of Java byte[]

Tags:

flutter

dart

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?

like image 883
Neigaard Avatar asked Feb 27 '18 19:02

Neigaard


People also ask

What is byte [] in Java?

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.

How do you convert int to byte in darts?

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.

What is Uint8List in Dart?

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.

Can you store bytes in JSON?

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.


2 Answers

Here is the standard type mapping for platform channels:

https://flutter.io/platform-channels/#codec

On Android, byte[] maps to Uint8List.

like image 197
Daniel Chesher Avatar answered Oct 31 '22 18:10

Daniel Chesher


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

like image 42
matanlurey Avatar answered Oct 31 '22 18:10

matanlurey