@ohos.data.preferences (User Preferences)

The user preferences module provides APIs for processing data in the form of key-value (KV) pairs and supports persistence of the KV pairs when required.

The key is of the string type, and the value can be a number, a string, a Boolean value, or an array of numbers, strings, or Boolean values.

NOTE

The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import data_preferences from '@ohos.data.preferences';

Constants

System capability: SystemCapability.DistributedDataManager.Preferences.Core

Name Type Readable Writable Description
MAX_KEY_LENGTH number Yes No Maximum length of a key, which is 80 bytes.
MAX_VALUE_LENGTH number Yes No Maximum length of a value, which is 8192 bytes.

data_preferences.getPreferences

getPreferences(context: Context, name: string, callback: AsyncCallback<Preferences>): void

Obtains a Preferences instance. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.DistributedDataManager.Preferences.Core

Parameters

Name Type Mandatory Description
context Context Yes Application context.
For details about the application context of the FA model, see Context.
For details about the application context of the stage model, see Context.
name string Yes Name of the Preferences instance.
callback AsyncCallback<Preferences> Yes Callback invoked to return the result. If the operation is successful, err is undefined and object is the Preferences instance obtained. Otherwise, err is an error code.

Example

FA model:

// Obtain the context.
import featureAbility from '@ohos.ability.featureAbility';
let context = featureAbility.getContext();
let preferences = null;

try {
    data_preferences.getPreferences(context, 'mystore', function (err, val) {
        if (err) {
	        console.info("Failed to obtain the preferences. code =" + err.code + ", message =" + err.message);
	        return;
	    }
	    preferences = val;
	    console.info("Obtained the preferences successfully.");
	})
} catch (err) {
    console.info("Failed to obtain the preferences. code =" + err.code + ", message =" + err.message);
}

Stage model:

import UIAbility from '@ohos.app.ability.UIAbility';

let preferences = null;

class EntryAbility extends UIAbility {
    onWindowStageCreate(windowStage) {
        try {
            data_preferences.getPreferences(this.context, 'mystore', function (err, val) {
                if (err) {
                    console.info("Failed to obtain the preferences. code =" + err.code + ", message =" + err.message);
                    return;
                }
                preferences = val;
                console.info("Obtained the preferences successfully.");
            })
        } catch (err) {
            console.info("Failed to obtain the preferences. code =" + err.code + ", message =" + err.message);
        }
    }
}

data_preferences.getPreferences

getPreferences(context: Context, name: string): Promise<Preferences>

Obtains a Preferences instance. This API uses a promise to return the result.

System capability: SystemCapability.DistributedDataManager.Preferences.Core

Parameters

Name Type Mandatory Description
context Context Yes Application context.
For details about the application context of the FA model, see Context.
For details about the application context of the stage model, see Context.
name string Yes Name of the Preferences instance.

Return value

Type Description
Promise<Preferences> Promise used to return the Preferences instance obtained.

Example

FA model:

// Obtain the context.
import featureAbility from '@ohos.ability.featureAbility';
let context = featureAbility.getContext();

let preferences = null;
try {
    let promise = data_preferences.getPreferences(context, 'mystore');
    promise.then((object) => {
        preferences = object;
        console.info("Obtained the preferences successfully.");
    }).catch((err) => {
        console.info("Failed to obtain the preferences. code =" + err.code + ", message =" + err.message);
    })
} catch(err) {
    console.info("Failed to obtain the preferences. code =" + err.code + ", message =" + err.message);
}

Stage model:

import UIAbility from '@ohos.app.ability.UIAbility';

let preferences = null;

class EntryAbility extends UIAbility {
    onWindowStageCreate(windowStage) {
        try {
            let promise = data_preferences.getPreferences(this.context, 'mystore');
            promise.then((object) => {
                preferences = object;
                console.info("Obtained the preferences successfully.");
            }).catch((err) => {
                console.info("Failed to obtain the preferences. code =" + err.code + ", message =" + err.message);
            })
        } catch(err) {
            console.info("Failed to obtain the preferences. code =" + err.code + ", message =" + err.message);
        }
    }
}

data_preferences.deletePreferences

deletePreferences(context: Context, name: string, callback: AsyncCallback<void>): void

Deletes a Preferences instance from the memory. This API uses an asynchronous callback to return the result.

If the Preferences instance has a persistent file, this API also deletes the persistent file.

The deleted Preferences instance cannot be used for data operations. Otherwise, data inconsistency will be caused.

System capability: SystemCapability.DistributedDataManager.Preferences.Core

Parameters

Name Type Mandatory Description
context Context Yes Application context.
For details about the application context of the FA model, see Context.
For details about the application context of the stage model, see Context.
name string Yes Name of the Preferences instance to delete.
callback AsyncCallback<void> Yes Callback invoked to return the result. If the operation is successful, err is undefined. Otherwise, err is an error code.

Error codes

For details about the error codes, see User Preference Error Codes.

ID Error Message
15500010 Failed to delete preferences.

Example

FA model:

// Obtain the context.
import featureAbility from '@ohos.ability.featureAbility';
let context = featureAbility.getContext();

try {
    data_preferences.deletePreferences(context, 'mystore', function (err) {
        if (err) {
            console.info("Failed to delete the preferences. code =" + err.code + ", message =" + err.message);
            return;
        }
        console.info("Deleted the preferences successfully." );
    })
} catch (err) {
    console.info("Failed to delete the preferences. code =" + err.code + ", message =" + err.message);
}

Stage model:

import UIAbility from '@ohos.app.ability.UIAbility';

class EntryAbility extends UIAbility {
    onWindowStageCreate(windowStage) {
        try {
            data_preferences.deletePreferences(this.context, 'mystore', function (err) {
                if (err) {
                    console.info("Failed to delete the preferences. code =" + err.code + ", message =" + err.message);
                    return;
                }
                console.info("Deleted the preferences successfully." );
            })
        } catch (err) {
            console.info("Failed to delete the preferences. code =" + err.code + ", message =" + err.message);
        }
    }
}

data_preferences.deletePreferences

deletePreferences(context: Context, name: string): Promise<void>

Deletes a Preferences instance from the memory. This API uses a promise to return the result.

If the Preferences instance has a persistent file, this API also deletes the persistent file.

The deleted Preferences instance cannot be used for data operations. Otherwise, data inconsistency will be caused.

System capability: SystemCapability.DistributedDataManager.Preferences.Core

Parameters

Name Type Mandatory Description
context Context Yes Application context.
For details about the application context of the FA model, see Context.
For details about the application context of the stage model, see Context.
name string Yes Name of the Preferences instance to delete.

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see User Preference Error Codes.

ID Error Message
15500010 Failed to delete preferences.

Example

FA model:

// Obtain the context.
import featureAbility from '@ohos.ability.featureAbility';
let context = featureAbility.getContext();

try {
    let promise = data_preferences.deletePreferences(context, 'mystore');
    promise.then(() => {
        console.info("Deleted the preferences successfully.");
    }).catch((err) => {
        console.info("Failed to delete the preferences. code =" + err.code + ", message =" + err.message);
    })
} catch(err) {
    console.info("Failed to delete the preferences. code =" + err.code + ", message =" + err.message);
}

Stage model:

import UIAbility from '@ohos.app.ability.UIAbility';

class EntryAbility extends UIAbility {
    onWindowStageCreate(windowStage) {
        try{
            let promise = data_preferences.deletePreferences(this.context, 'mystore');
            promise.then(() => {
                console.info("Deleted the preferences successfully.");
            }).catch((err) => {
                console.info("Failed to delete the preferences. code =" + err.code + ", message =" + err.message);
            })
        } catch(err) {
            console.info("Failed to delete the preferences. code =" + err.code + ", message =" + err.message);
        }
    }
}

data_preferences.removePreferencesFromCache

removePreferencesFromCache(context: Context, name: string, callback: AsyncCallback<void>): void

Removes a Preferences instance from the memory. This API uses an asynchronous callback to return the result.

The removed Preferences instance cannot be used for data operations. Otherwise, data inconsistency will be caused.

System capability: SystemCapability.DistributedDataManager.Preferences.Core

Parameters

Name Type Mandatory Description
context Context Yes Application context.
For details about the application context of the FA model, see Context.
For details about the application context of the stage model, see Context.
name string Yes Name of the Preferences instance to remove.
callback AsyncCallback<void> Yes Callback invoked to return the result. If the operation is successful, err is undefined. Otherwise, err is an error code.

Example

FA model:

// Obtain the context.
import featureAbility from '@ohos.ability.featureAbility';
let context = featureAbility.getContext();

try {
    data_preferences.removePreferencesFromCache(context, 'mystore', function (err) {
        if (err) {
            console.info("Failed to remove the preferences. code =" + err.code + ", message =" + err.message);
            return;
        }
        console.info("Removed the preferences successfully.");
    })
} catch (err) {
    console.info("Failed to remove the preferences. code =" + err.code + ", message =" + err.message);
}

Stage model:

import UIAbility from '@ohos.app.ability.UIAbility';

class EntryAbility extends UIAbility {
    onWindowStageCreate(windowStage) {
        try {
            data_preferences.removePreferencesFromCache(this.context, 'mystore', function (err) {
                if (err) {
                    console.info("Failed to remove the preferences. code =" + err.code + ", message =" + err.message);
                    return;
                }
                console.info("Removed the preferences successfully.");
            })
        } catch (err) {
            console.info("Failed to remove the preferences. code =" + err.code + ", message =" + err.message);
        }
    }
}

data_preferences.removePreferencesFromCache

removePreferencesFromCache(context: Context, name: string): Promise<void>

Removes a Preferences instance from the memory. This API uses a promise to return the result.

The removed Preferences instance cannot be used for data operations. Otherwise, data inconsistency will be caused.

System capability: SystemCapability.DistributedDataManager.Preferences.Core

Parameters

Name Type Mandatory Description
context Context Yes Application context.
For details about the application context of the FA model, see Context.
For details about the application context of the stage model, see Context.
name string Yes Name of the Preferences instance to remove.

Return value

Type Description
Promise<void> Promise that returns no value.

Example

FA model:

// Obtain the context.
import featureAbility from '@ohos.ability.featureAbility';
let context = featureAbility.getContext();

try {
    let promise = data_preferences.removePreferencesFromCache(context, 'mystore');
	promise.then(() => {
    	console.info("Removed the preferences successfully.");
    }).catch((err) => {
        console.info("Failed to remove the preferences. code =" + err.code + ", message =" + err.message);
    })
} catch(err) {
    console.info("Failed to remove the preferences. code =" + err.code + ", message =" + err.message);
}

Stage model:

import UIAbility from '@ohos.app.ability.UIAbility';

class EntryAbility extends UIAbility {
    onWindowStageCreate(windowStage) {
        try {
            let promise = data_preferences.removePreferencesFromCache(this.context, 'mystore');
            promise.then(() => {
                console.info("Removed the preferences successfully.");
            }).catch((err) => {
                console.info("Failed to remove the preferences. code =" + err.code + ", message =" + err.message);
            })
        } catch(err) {
            console.info("Failed to remove the preferences. code =" + err.code + ", message =" + err.message);
        }
    }
}

Preferences

Provides methods for obtaining and modifying data.

Before calling any method of Preferences, you must obtain a Preferences instance by using data_preferences.getPreferences.

get

get(key: string, defValue: ValueType, callback: AsyncCallback<ValueType>): void

Obtains the value of a key. This API uses an asynchronous callback to return the result. If the value is null or is not of the default value type, defValue is returned.

System capability: SystemCapability.DistributedDataManager.Preferences.Core

Parameters

Name Type Mandatory Description
key string Yes Key of the data to obtain. It cannot be empty.
defValue ValueType Yes Default value to be returned. The value can be a number, a string, a Boolean value, or an array of numbers, strings, or Boolean values.
callback AsyncCallback<ValueType> Yes Callback invoked to return the result. If the operation is successful, err is** undefined** and data is the value obtained. Otherwise, err is an error code.

Example

try {
    preferences.get('startup', 'default', function (err, val) {
        if (err) {
            console.info("Failed to obtain the value of 'startup'. code =" + err.code + ", message =" + err.message);
            return;
        }
        console.info("Obtained the value of 'startup' successfully. val: " + val);
    })
} catch (err) {
    console.info("Failed to obtain the value of 'startup'. code =" + err.code + ", message =" + err.message);
}

get

get(key: string, defValue: ValueType): Promise<ValueType>

Obtains the value of a key. This API uses a promise to return the result. If the value is null or is not of the default value type, defValue is returned.

System capability: SystemCapability.DistributedDataManager.Preferences.Core

Parameters

Name Type Mandatory Description
key string Yes Key of the data to obtain. It cannot be empty.
defValue ValueType Yes Default value to be returned. The value can be a number, a string, a Boolean value, or an array of numbers, strings, or Boolean values.

Return value

Type Description
Promise<ValueType> Promise used to return the value obtained.

Example

try {
    let promise = preferences.get('startup', 'default');
    promise.then((data) => {
        console.info("Got the value of 'startup'. Data: " + data);
    }).catch((err) => {
        console.info("Failed to obtain the value of 'startup'. code =" + err.code + ", message =" + err.message);
    })
} catch(err) {
    console.info("Failed to obtain the value of 'startup'. code =" + err.code + ", message =" + err.message);
}

getAll

getAll(callback: AsyncCallback<Object>): void;

Obtains an Object instance that contains all KV pairs. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.DistributedDataManager.Preferences.Core

Parameters

Name Type Mandatory Description
callback AsyncCallback<Object> Yes Callback invoked to return the result. If the operation is successful, err is undefined and value is the Object instance obtained. Otherwise, err is an error code.

Example

try {
    preferences.getAll(function (err, value) {
        if (err) {
            console.info("Failed to get all KV pairs. code =" + err.code + ", message =" + err.message);
            return;
        }
    let allKeys = Object.keys(value);
    console.info("getAll keys = " + allKeys);
    console.info("getAll object = " + JSON.stringify(value));
    })
} catch (err) {
    console.info("Failed to get all KV pairs. code =" + err.code + ", message =" + err.message);
}

getAll

getAll(): Promise<Object>

Obtains an Object instance that contains all KV pairs. This API uses a promise to return the result.

System capability: SystemCapability.DistributedDataManager.Preferences.Core

Return value

Type Description
Promise<Object> Promise used to return the Object instance obtained.

Example

try {
    let promise = preferences.getAll();
    promise.then((value) => {
        let allKeys = Object.keys(value);
        console.info('getAll keys = ' + allKeys);
        console.info("getAll object = " + JSON.stringify(value));
    }).catch((err) => {
        console.info("Failed to get all KV pairs. code =" + err.code + ", message =" + err.message);
    })
} catch (err) {
    console.info("Failed to get all KV pairs. code =" + err.code + ", message =" + err.message);
}

put

put(key: string, value: ValueType, callback: AsyncCallback<void>): void

Writes data to this Preferences instance. This API uses an asynchronous callback to return the result. You can use flush to make the Preferences instance persistent.

System capability: SystemCapability.DistributedDataManager.Preferences.Core

Parameters

Name Type Mandatory Description
key string Yes Key of the data. It cannot be empty.
value ValueType Yes Value to write. The value can be a number, a string, a Boolean value, or an array of numbers, strings, or Boolean values.
callback AsyncCallback<void> Yes Callback invoked to return the result. If the operation is successful, err is undefined. Otherwise, err is an error code.

Example

try {
    preferences.put('startup', 'auto', function (err) {
        if (err) {
            console.info("Failed to put the value of 'startup'. code =" + err.code + ", message =" + err.message);
            return;
        }
        console.info("Put the value of 'startup' successfully.");
    })
} catch (err) {
    console.info("Failed to put the value of 'startup'. code =" + err.code + ", message =" + err.message);
}

put

put(key: string, value: ValueType): Promise<void>

Writes data to this Preferences instance. This API uses a promise to return the result. You can use flush to make the Preferences instance persistent.

System capability: SystemCapability.DistributedDataManager.Preferences.Core

Parameters

Name Type Mandatory Description
key string Yes Key of the data. It cannot be empty.
value ValueType Yes Value to write. The value can be a number, a string, a Boolean value, or an array of numbers, strings, or Boolean values.

Return value

Type Description
Promise<void> Promise that returns no value.

Example

try {
    let promise = preferences.put('startup', 'auto');
    promise.then(() => {
        console.info("Put the value of 'startup' successfully.");
    }).catch((err) => {
        console.info("Failed to put the value of 'startup'. code =" + err.code +", message =" + err.message);
    })
} catch(err) {
    console.info("Failed to put the value of 'startup'. code =" + err.code +", message =" + err.message);
}

has

has(key: string, callback: AsyncCallback<boolean>): void

Checks whether this Preferences instance contains a KV pair with the given key. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.DistributedDataManager.Preferences.Core

Parameters

Name Type Mandatory Description
key string Yes Key of the data to check. It cannot be empty.
callback AsyncCallback<boolean> Yes Callback invoked to return the result. If the Preferences instance contains the KV pair, true will be returned. Otherwise, false will be returned.

Example

try {
    preferences.has('startup', function (err, val) {
        if (err) {
            console.info("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message);
            return;
        }
        if (val) {
            console.info("The key 'startup' is contained.");
        } else {
            console.info("The key 'startup' is not contained.");
        }
  })
} catch (err) {
    console.info("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message);
}

has

has(key: string): Promise<boolean>

Checks whether this Preferences instance contains a KV pair with the given key. This API uses a promise to return the result.

System capability: SystemCapability.DistributedDataManager.Preferences.Core

Parameters

Name Type Mandatory Description
key string Yes Key of the data to check. It cannot be empty.

Return value

Type Description
Promise<boolean> Promise used to return the result. If the Preferences instance contains the KV pair, true will be returned. Otherwise, false will be returned.

Example

try {
    let promise = preferences.has('startup');
    promise.then((val) => {
        if (val) {
            console.info("The key 'startup' is contained.");
        } else {
            console.info("The key 'startup' is not contained.");
        }
    }).catch((err) => {
        console.info("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message);
  })
} catch(err) {
    console.info("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message);
}

delete

delete(key: string, callback: AsyncCallback<void>): void

Deletes a KV pair from this Preferences instance based on the specified key. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.DistributedDataManager.Preferences.Core

Parameters

Name Type Mandatory Description
key string Yes Key of the KV pair to delete. It cannot be empty.
callback AsyncCallback<void> Yes Callback invoked to return the result. If the operation is successful, err is undefined. Otherwise, err is an error code.

Example

try {
    preferences.delete('startup', function (err) {
        if (err) {
            console.info("Failed to delete the key 'startup'. code =" + err.code + ", message =" + err.message);
            return;
        }
        console.info("Deleted the key 'startup'.");
    })
} catch (err) {
    console.info("Failed to delete the key 'startup'. code =" + err.code + ", message =" + err.message);
}

delete

delete(key: string): Promise<void>

Deletes a KV pair from this Preferences instance. This API uses a promise to return the result.

System capability: SystemCapability.DistributedDataManager.Preferences.Core

Parameters

Name Type Mandatory Description
key string Yes Key of the KV pair to delete. It cannot be empty.

Return value

Type Description
Promise<void> Promise that returns no value.

Example

try {
    let promise = preferences.delete('startup');
	promise.then(() => {
        console.info("Deleted the key 'startup'.");
    }).catch((err) => {
        console.info("Failed to delete the key 'startup'. code =" + err.code +", message =" + err.message);
    })
} catch(err) {
    console.info("Failed to delete the key 'startup'. code =" + err.code +", message =" + err.message);
}

flush

flush(callback: AsyncCallback<void>): void

Saves the data of this Preferences instance to a file asynchronously. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.DistributedDataManager.Preferences.Core

Parameters

Name Type Mandatory Description
callback AsyncCallback<void> Yes Callback invoked to return the result. If the operation is successful, err is undefined. Otherwise, err is an error code.

Example

try {
    preferences.flush(function (err) {
        if (err) {
            console.info("Failed to flush data. code =" + err.code + ", message =" + err.message);
            return;
        }
        console.info("Flushed data successfully.");
    })
} catch (err) {
    console.info("Failed to flush data. code =" + err.code + ", message =" + err.message);
}

flush

flush(): Promise<void>

Saves the data of this Preferences instance to a file asynchronously. This API uses a promise to return the result.

System capability: SystemCapability.DistributedDataManager.Preferences.Core

Return value

Type Description
Promise<void> Promise that returns no value.

Example

try {
    let promise = preferences.flush();
    promise.then(() => {
        console.info("Flushed data successfully.");
    }).catch((err) => {
        console.info("Failed to flush data. code =" + err.code + ", message =" + err.message);
    })
} catch (err) {
    console.info("Failed to flush data. code =" + err.code + ", message =" + err.message);
}

clear

clear(callback: AsyncCallback<void>): void

Clears this Preferences instance. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.DistributedDataManager.Preferences.Core

Parameters

Name Type Mandatory Description
callback AsyncCallback<void> Yes Callback invoked to return the result. If the operation is successful, err is undefined. Otherwise, err is an error code.

Example

try {
	preferences.clear(function (err) {
        if (err) {
            console.info("Failed to clear data. code =" + err.code + ", message =" + err.message);
            return;
        }
        console.info("Cleared data successfully.");
    })
} catch (err) {
    console.info("Failed to clear data. code =" + err.code + ", message =" + err.message);
}

clear

clear(): Promise<void>

Clears this Preferences instance. This API uses a promise to return the result.

System capability: SystemCapability.DistributedDataManager.Preferences.Core

Return value

Type Description
Promise<void> Promise that returns no value.

Example

try {
    let promise = preferences.clear();
	promise.then(() => {
    	console.info("Cleared data successfully.");
    }).catch((err) => {
        console.info("Failed to clear data. code =" + err.code + ", message =" + err.message);
    })
} catch(err) {
    console.info("Failed to clear data. code =" + err.code + ", message =" + err.message);
}

on('change')

on(type: 'change', callback: Callback<{ key : string }>): void

Subscribes to data changes. A callback will be triggered to return the new value if the value of the subscribed key is changed and flushed.

System capability: SystemCapability.DistributedDataManager.Preferences.Core

Parameters

Name Type Mandatory Description
type string Yes Event type to subscribe to. The value change indicates data change events.
callback Callback<{ key : string }> Yes Callback invoked to return data changes.

Example

try {
	data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
		if (err) {
			console.info("Failed to obtain the preferences.");
			return;
		}
		let observer = function (key) {
			console.info("The key " + key + " changed.");
		}
		preferences.on('change', observer);
		preferences.put('startup', 'manual', function (err) {
			if (err) {
				console.info("Failed to put the value of 'startup'. Cause: " + err);
				return;
			}
			console.info("Put the value of 'startup' successfully.");

			preferences.flush(function (err) {
				if (err) {
					console.info("Failed to flush data. Cause: " + err);
					return;
				}
				console.info("Flushed data successfully.");
			})
		})
	})
} catch (err) {
	console.info("Failed to flush data. code =" + err.code + ", message =" + err.message);
}

off('change')

off(type: 'change', callback?: Callback<{ key : string }>): void

Unsubscribes from data changes.

System capability: SystemCapability.DistributedDataManager.Preferences.Core

Parameters

Name Type Mandatory Description
type string Yes Event type to unsubscribe from. The value change indicates data change events.
callback Callback<{ key : string }> No Callback to unregister. If this parameter is left blank, the callbacks for all data changes will be unregistered.

Example

try {
    data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
        if (err) {
            console.info("Failed to obtain the preferences.");
            return;
        }
        let observer = function (key) {
            console.info("The key " + key + " changed.");
        }
        preferences.on('change', observer);
        preferences.put('startup', 'auto', function (err) {
            if (err) {
                console.info("Failed to put the value of 'startup'. Cause: " + err);
                return;
            }
            console.info("Put the value of 'startup' successfully.");

            preferences.flush(function (err) {
                if (err) {
                    console.info("Failed to flush data. Cause: " + err);
                    return;
                }
                console.info("Flushed data successfully.");
            })
            preferences.off('change', observer);
        })
    })
} catch (err) {
    console.info("Failed to flush data. code =" + err.code + ", message =" + err.message);
}

ValueType

Enumerates the value types.

System capability: SystemCapability.DistributedDataManager.Preferences.Core

Type Description
number The value is a number.
string The value is a string.
boolean The value is of Boolean type.
Array<number> The value is an array of numbers.
Array<boolean> The value is a Boolean array.
Array<string> The value is an array of the strings.