setDefaults method

  1. @override
Future<RequestResponse> setDefaults({
  1. required String key,
  2. required dynamic value,
  3. String? parent = '__default',
})
override

Sets a default value in backend for a specified key and return nothing if successful.

The value can only be String, bool, num, Map or List. Otherwise, InvalidDefaultsValue will be thrown.

Can specify the parent if the key is duplicated under a different parent.

Throws NotLoggedInUser if the user is not logged in.

Implementation

@override
Future<RequestResponse<dynamic>> setDefaults(
    {required String key,
    required dynamic value,
    String? parent = '__default'}) async {
  if (!config.coreInstance.auth.isLoggedIn) throw NotLoggedInUser();

  dynamic _value;

  if (_renovationCustomSettings.contains(key)) {
    key = 'renovation:$key';
  }

  if (value == null ||
      value is! String &&
          value is! Map &&
          value is! List &&
          value is! bool &&
          value is! num) {
    throw InvalidDefaultsValue();
  }

  if (value is! String) {
    if (value is Map || value is List) {
      _value = jsonEncode(value);
    } else if (value is bool) {
      _value = value.toString();
    } else if (value is num) {
      _value = value.toString();
    }
  } else {
    _value = value;
  }

  final response = await Request.initiateRequest(
      url: '${config.hostUrl}/api/method/frappe.client.set_default',
      method: HttpMethod.POST,
      isFrappeResponse: false,
      contentType: ContentTypeLiterals.APPLICATION_X_WWW_FORM_URLENCODED,
      data: <String, dynamic>{'key': key, 'value': _value, 'parent': parent});

  if (response.isSuccess) {
    return RequestResponse.success<dynamic>(response.data!.message);
  } else {
    return RequestResponse.fail<dynamic>(handleError(null, response.error!));
  }
}