setUserKey method

Future<void> setUserKey(
  1. {String userKey = '',
  2. String userReKey = ''}
)

This method is called to set or modify the user password for Cloud DB full encryption. For the cache mode, you are advised to enable local database encryption on the device to ensure local data security. If the object type you create on the AppGallery Connect console contains encrypted fields but you do not use setUserKey() to set the user password, the data of this object type cannot be synchronized to the cloud and you cannot perform operations on the data of this object type on the cloud.

The options are as follows:

  • If the value of userKey is NOT an empty string and the value of userReKey is an empty string, set full encryption for Cloud DB.
  • If the value of userKey is NOT an empty string and the value of userReKey is NOT an empty string, change the original full encryption password to a new one for Cloud DB.
  • If the value of userKey is an empty string, the input parameter is invalid and the system will report an error.

userKey

  • User password, which is a string of 8 to 32 characters and can contain only digits, lowercase letters, uppercase letters, spaces, and special characters.

userReKey

  • New user password, which is a string of 8 to 32 characters and can contain only digits, lowercase letters, uppercase letters, spaces, and special characters.
  • This parameter is required when the password is changed.

https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-References/clouddb-agconnectclouddb#Specialcharacters

Implementation

Future<void> setUserKey({String userKey = '', String userReKey = ''}) async {
  if (userKey == null) {
    throw FormatException('Value of userKey cannot be null.', userKey);
  }
  if (userReKey == null) {
    throw FormatException('Value of userReKey cannot be null.', userReKey);
  }

  try {
    return await _methodChannel.invokeMethod<void>(
      MethodConstants.SET_USER_KEY,
      <String, dynamic>{
        KeyConstants.USER_KEY: userKey,
        KeyConstants.USER_RE_KEY: userReKey,
      },
    );
  } catch (e) {
    throw AGConnectCloudDBException.from(e);
  }
}