Packet class

This class models the raw data that needs to be sent or received over the network. and is just a wrapper around the raw bytes/stream.

Packets can serialize and deserialize formats supported by jsonEncode using Packet.from. When using Packet.from, it assumes that the enclosed data is safe for utf8 encoding and decoding.

If you want to create a Packet from raw bytes, you have two options:

  1. If you already know all the bytes, you can use Packet.fromBytes.
  2. If you don't yet have all of the bytes, you can use the Packet constructor and keep adding bytes using the add or addAll methods

If you want to create a Packet from stream, use the Packet.fromStream constructor.

Constructors

Packet()
Packet.from(dynamic data)
Allow Packets to be created from different data types. Supported data types are: String, numbers, Map, List, and Uint8List. Complex data types such as Map and List must be supported by jsonEncode
factory
Packet.fromBytes(List<int> bytes)
Allow Packets to be created/copied from other bytes
factory
Packet.fromStream(Stream<List<int>> stream)
Allow Packets to be created from Stream
factory

Properties

bytes Uint8List
Get the raw bytes in this packet
read-only
hashCode int
The hash code for this object. [...]
read-only, inherited
isStream bool
Check is this packet is enclosing a stream
read-only
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited
size int
Get the size of the bytes in this packet. This only works if the packet is not a stream.
read-only
stream Stream
Get the enclosing stream
read-only

Methods

add(int byte) → dynamic
Add a single by to this packet
addAll(Iterable<int> bytes) → dynamic
Add bytes to this packet
as<E>() → E
Allow transformation back to popular data types For using the as() and asXX() methods, We're assuming it is safe to transform to utf8 Strings. [...]
asBool() bool
For using this method, We're assuming it is safe to transform to utf8 Strings. Returns the bytes as a boolean using string comparison
asDouble() double
For using this method, We're assuming it is safe to transform to utf8 Strings. Returns the bytes as a double using double.parse()
asInt() int
For using this method, We're assuming it is safe to transform to utf8 Strings. Returns the bytes as am integer using int.parse()
asList<E>() List<E>
For using this method, We're assuming it is safe to transform to utf8 Strings. Return the bytes as a List decoded using jsonDecode(). Same as as(). If the List contains all String, you can do asList() or get the dynamic form and do the cast yourself.
asMap<E, F>() Map<E, F>
For using this method, We're assuming it is safe to transform to utf8 Strings. Return the bytes as a Map decoded using jsonDecode(). Same as as(). If the Key and Value are Strings, you can do asMap<String, String>() or get the dynamic form and do the cast yourself.
asNumber() num
For using this method, We're assuming it is safe to transform to utf8 Strings. Returns the bytes as a number using num.parse()
asString() String
For using this method, We're assuming it is safe to transform to utf8 Strings. Return the bytes as utf8 encoded string. Same as as().
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
toString() String
Returns a string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator. [...]
inherited