read<T> static method

Future read<T>(
  1. String key, {
  2. dynamic defaultValue,
  3. Map<Type, dynamic>? modelDecoders,
})

Read a value from the local storage

Implementation

static Future<dynamic> read<T>(String key,
    {dynamic defaultValue, Map<Type, dynamic>? modelDecoders}) async {
  String? data = await manager().read(key: key);
  String? runtimeType = await manager().read(key: "${key}_runtime_type");

  if (data == null) {
    return defaultValue;
  }

  if (runtimeType != null && modelDecoders == null) {
    switch (runtimeType.toLowerCase()) {
      case 'int':
        return int.parse(data);
      case 'double':
        return double.parse(data);
      case 'String':
        return data;
      case 'bool':
        return data == 'true';
      case 'json':
        try {
          return jsonDecode(data);
        } on Exception catch (e) {
          NyLogger.error(e.toString());
          return null;
        }
    }
  } else {
    if (T.toString() == "String") {
      return data.toString();
    }

    if (T.toString() == "int") {
      return int.parse(data.toString());
    }

    if (T.toString() == "double") {
      return double.parse(data);
    }

    if (_isInteger(data)) {
      return int.parse(data);
    }

    if (_isDouble(data)) {
      return double.parse(data);
    }
  }

  if (T.toString() != 'dynamic') {
    try {
      return dataToModel<T>(
          data: jsonDecode(data), modelDecoders: modelDecoders);
    } on Exception catch (e) {
      NyLogger.error(e.toString());
      return null;
    }
  }
  return data;
}