encodeBytes method

bool encodeBytes(
  1. String key,
  2. MMBuffer? value, [
  3. int? expireDurationInSecond
])

Encoding bytes.

You can serialize an object into bytes, then store it inside MMKV.

// assume using protobuf https://developers.google.com/protocol-buffers/docs/darttutorial
var object = MyClass();
final list = object.writeToBuffer();
final buffer = MMBuffer.fromList(list);

mmkv.encodeBytes('bytes', buffer);

buffer.destroy();

expireDurationInSecond override the default duration setting from enableAutoKeyExpire().

Implementation

bool encodeBytes(String key, MMBuffer? value, [int? expireDurationInSecond]) {
  if (value == null) {
    removeValue(key);
    return true;
  }

  final keyPtr = key.toNativeUtf8();
  final ret = (expireDurationInSecond == null)
      ? _encodeBytes(_handle, keyPtr, value.pointer!, value.length)
      : _encodeBytesV2(_handle, keyPtr, value.pointer!, value.length, expireDurationInSecond);
  calloc.free(keyPtr);
  return _int2Bool(ret);
}