Dart DocumentationsqljockyBlob

Blob class

Holds blob data, and can be created or accessed as either a String or a List of 8-bit integers.

When a blob which was created as a list of integers is accessed as a string, those integers are treated as UTF-8 code units (unsigned 8-bit integers).

class Blob {
 String _string;
 List<int> _codeUnits;
 int _hashcode;
 
 /// Create a [Blob] from a [string].
 Blob.fromString(String string) {
   this._string = string;
 }
 
 /// Create a [Blob] from a list of [codeUnits].
 Blob.fromBytes(List<int> codeUnits) {
   this._codeUnits = codeUnits;
 }
 
 /// Returns the value of the blob as a [String].
 String toString() {
   if (_string != null) {
     return _string;
   }
   return UTF8.decode(_codeUnits);
 }
 
 /// Returns the value of the blob as a list of code units.
 List<int> toBytes() {
   if (_codeUnits != null) {
     return _codeUnits;
   }
   return UTF8.encode(_string);
 }
 
 int get hashCode {
   if (_hashcode == null) {
     if (_string != null) {
       _hashcode = _string.hashCode;
     } else {
       _hashcode = UTF8.decode(_codeUnits).hashCode;
     }
   }
   return _hashcode;
 }
 
 bool operator ==(other) => toString() == other.toString();
}

Constructors

new Blob.fromBytes(List<int> codeUnits) #

Create a Blob from a list of codeUnits.

Blob.fromBytes(List<int> codeUnits) {
 this._codeUnits = codeUnits;
}

new Blob.fromString(String string) #

Create a Blob from a string.

Blob.fromString(String string) {
 this._string = string;
}

Properties

final int hashCode #

Get a hash code for this object.

All objects have hash codes. Hash codes are guaranteed to be the same for objects that are equal when compared using the equality operator ==. Other than that there are no guarantees about the hash codes. They will not be consistent between runs and there are no distribution guarantees.

If a subclass overrides hashCode it should override the equality operator as well to maintain consistency.

docs inherited from Object
int get hashCode {
 if (_hashcode == null) {
   if (_string != null) {
     _hashcode = _string.hashCode;
   } else {
     _hashcode = UTF8.decode(_codeUnits).hashCode;
   }
 }
 return _hashcode;
}

Operators

bool operator ==(other) #

The equality operator.

The default behavior for all Objects is to return true if and only if this and other are the same object.

Override this method to specify a different equality relation on a class. The overriding method must still be an equivalence relation. That is, it must be:

  • Total: It must return a boolean for all arguments. It should never throw or return null.

  • Reflexive: For all objects o, o == o must be true.

  • Symmetric: For all objects o1 and o2, o1 == o2 and o2 == o1 must either both be true, or both be false.

  • Transitive: For all objects o1, o2, and o3, if o1 == o2 and o2 == o3 are true, then o1 == o3 must be true.

The method should also be consistent over time, so equality of two objects should not change over time, or at least only change if one of the objects was modified.

If a subclass overrides the equality operator it should override the hashCode method as well to maintain consistency.

docs inherited from Object
bool operator ==(other) => toString() == other.toString();

Methods

List<int> toBytes() #

Returns the value of the blob as a list of code units.

List<int> toBytes() {
 if (_codeUnits != null) {
   return _codeUnits;
 }
 return UTF8.encode(_string);
}

String toString() #

Returns the value of the blob as a String.

String toString() {
 if (_string != null) {
   return _string;
 }
 return UTF8.decode(_codeUnits);
}