putObject<T> method

Future<bool> putObject<T>(
  1. String key,
  2. T value
)

Implementation

Future<bool> putObject<T>(String key, T value) async {
  if (value == null) {
    return await remove(key);
  }

  if (value is String) {
    return await putString(key, value);
  } else if (value is bool) {
    return await putBoolean(key, value);
  } else if (value is int) {
    return await putInt(key, value);
  } else if (value is double) {
    return await putDouble(key, value);
  } else if (value is List<String>) {
    return await putStringList(key, value);
  } else if (value is Map || value is List) {
    return await putJson(key, value);
  } else {
    // Try to serialize as JSON
    try {
      final jsonStr = jsonEncode(value);
      return await putString(key, jsonStr);
    } catch (e) {
      debugPrint('Cannot serialize object of type ${value.runtimeType}');
      return false;
    }
  }
}