MMKV class

An efficient, small mobile key-value storage framework developed by WeChat. Works on Android & iOS.

Constructors

MMKV(String mmapID, {MMKVMode mode = MMKVMode.SINGLE_PROCESS_MODE, String? cryptKey, String? rootDir, int expectedCapacity = 0})
Get an MMKV instance with an unique ID mmapID.

Properties

actualSize int
Get the actual used size. See also totalSize.
no setter
allKeys List<String>
Get all the keys (unsorted).
no setter
allNonExpiredKeys List<String>
Get all non-expired keys (unsorted). Note that this call has costs.
no setter
count int
no setter
countNonExpiredKeys int
Get non-expired keys. Note that this call has costs.
no setter
cryptKey String?
See also reKey().
no setter
hashCode int
The hash code for this object.
no setterinherited
mmapID String
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
totalSize int
Get the file size. See also actualSize.
no setter

Methods

checkReSetCryptKey(String cryptKey) → void
Just reset the cryptKey (will not encrypt or decrypt anything). Usually you should call this method after other process reKey() the multi-process mmkv.
clearAll({bool keepSpace = false}) → void
clearMemoryCache() → void
Clear all caches (on memory warning).
close() → void
Close the instance when it's no longer needed in the near future. Any subsequent call to the instance is undefined behavior.
containsKey(String key) bool
decodeBool(String key, {bool defaultValue = false}) bool
decodeBytes(String key) MMBuffer?
Decoding bytes.
decodeDouble(String key, {double defaultValue = 0}) double
decodeInt(String key, {int defaultValue = 0}) int
decodeInt32(String key, {int defaultValue = 0}) int
Use this when the value won't be larger than a normal int32. It's more efficient & cost less space.
decodeString(String key) String?
Decode as an utf-8 string.
disableAutoKeyExpire() bool
Disable auto key expiration. This is a downgrade operation.
disableCompareBeforeSet() bool
Disable compare value before update/insert.
enableAutoKeyExpire(int expiredInSeconds) bool
Enable auto key expiration. This is a upgrade operation, the file format will change. And the file won't be accessed correctly by older version (v1.2.17) of MMKV. expireDurationInSecond the expire duration for all keys, MMKV.ExpireNever (0) means no default duration (aka each key will have it's own expire date)
enableCompareBeforeSet() bool
Enable compare value before update/insert.
encodeBool(String key, bool value, [int? expireDurationInSecond]) bool
expireDurationInSecond override the default duration setting from enableAutoKeyExpire().
encodeBytes(String key, MMBuffer? value, [int? expireDurationInSecond]) bool
Encoding bytes.
encodeDouble(String key, double value, [int? expireDurationInSecond]) bool
expireDurationInSecond override the default duration setting from enableAutoKeyExpire().
encodeInt(String key, int value, [int? expireDurationInSecond]) bool
expireDurationInSecond override the default duration setting from enableAutoKeyExpire().
encodeInt32(String key, int value, [int? expireDurationInSecond]) bool
Use this when the value won't be larger than a normal int32. It's more efficient & cost less space. expireDurationInSecond override the default duration setting from enableAutoKeyExpire().
encodeString(String key, String? value, [int? expireDurationInSecond]) bool
Encode an utf-8 string. expireDurationInSecond override the default duration setting from enableAutoKeyExpire().
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
reKey(String? cryptKey) bool
Change encryption key for the MMKV instance.
removeValue(String key) → void
removeValues(List<String> keys) → void
See also trim().
sync(bool sync) → void
Synchronize memory to file. You don't need to call this, really, I mean it. Unless you worry about running out of battery.
toString() String
A string representation of this object.
inherited
trim() → void
Trim the file size to minimal.
valueSize(String key, bool actualSize) int
Get the actual size consumption of the key's value. Pass actualSize with true to get value's length.
writeValueToNativeBuffer(String key, MMBuffer buffer) int
Write the value to a pre-allocated native buffer.

Operators

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

Static Properties

pageSize int
Get memory page size.
no setter
rootDir String
The root directory of MMKV.
no setter
version String
no setter

Static Methods

backupAllToDirectory(String dstDir) int
backup all MMKV instance to dstDir
backupOneToDirectory(String mmapID, String dstDir, {String? rootDir}) bool
backup one MMKV instance to dstDir
defaultMMKV({String? cryptKey}) MMKV
A generic purpose instance in single-process mode.
initialize({String? rootDir, String? groupDir, MMKVLogLevel logLevel = MMKVLogLevel.Info}) Future<String>
MMKV must be initialized before any usage.
removeStorage(String mmapID, {String? rootDir}) bool
remove the storage of the MMKV, including the data file & meta file (.crc) Note: the existing instance (if any) will be closed & destroyed
restoreAllFromDirectory(String srcDir) int
restore all MMKV instance from srcDir
restoreOneMMKVFromDirectory(String mmapID, String srcDir, {String? rootDir}) bool
restore one MMKV instance from srcDir

Constants

ExpireInDay → const int
ExpireInHour → const int
ExpireInMinute → const int
ExpireInMonth → const int
ExpireInYear → const int
ExpireNever → const int