length property

int get length

Returns the length of the underlying FlexBuffer value. If the underlying value is null the length is 0. If the underlying value is a number, or a bool, the length is 1. If the underlying value is a vector, or map, the length reflects number of elements / element pairs. If the values is a string or a blob, the length reflects a number of bytes the value occupies (strings are encoded in utf8 format).

Implementation

int get length {
  if (_length == null) {
    // needs to be checked before more generic isAVector
    if (ValueTypeUtils.isFixedTypedVector(_valueType)) {
      _length = ValueTypeUtils.fixedTypedVectorElementSize(_valueType);
    } else if (_valueType == ValueType.Blob ||
        ValueTypeUtils.isAVector(_valueType) ||
        _valueType == ValueType.Map) {
      _length = _readUInt(
          _indirect - _byteWidth, BitWidthUtil.fromByteWidth(_byteWidth));
    } else if (_valueType == ValueType.Null) {
      _length = 0;
    } else if (_valueType == ValueType.String) {
      final indirect = _indirect;
      var sizeByteWidth = _byteWidth;
      var size = _readUInt(indirect - sizeByteWidth,
          BitWidthUtil.fromByteWidth(sizeByteWidth));
      while (_buffer.getInt8(indirect + size) != 0) {
        sizeByteWidth <<= 1;
        size = _readUInt(indirect - sizeByteWidth,
            BitWidthUtil.fromByteWidth(sizeByteWidth));
      }
      _length = size;
    } else if (_valueType == ValueType.Key) {
      final indirect = _indirect;
      var size = 1;
      while (_buffer.getInt8(indirect + size) != 0) {
        size += 1;
      }
      _length = size;
    } else {
      _length = 1;
    }
  }
  return _length!;
}