kv.dart 944 B

123456789101112131415161718192021222324252627282930313233
  1. import 'package:appflowy_backend/dispatch/dispatch.dart';
  2. import 'package:appflowy_backend/protobuf/flowy-config/entities.pb.dart';
  3. import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
  4. import 'package:dartz/dartz.dart';
  5. /// Key-value store
  6. /// The data is stored in the local storage of the device.
  7. class KeyValue {
  8. static Future<void> set(String key, String value) async {
  9. await ConfigEventSetKeyValue(
  10. KeyValuePB.create()
  11. ..key = key
  12. ..value = value,
  13. ).send();
  14. }
  15. static Future<Either<String, FlowyError>> get(String key) {
  16. return ConfigEventGetKeyValue(
  17. KeyPB.create()..key = key,
  18. ).send().then(
  19. (result) => result.fold(
  20. (pb) => left(pb.value),
  21. (error) => right(error),
  22. ),
  23. );
  24. }
  25. static Future<void> remove(String key) async {
  26. await ConfigEventRemoveKeyValue(
  27. KeyPB.create()..key = key,
  28. ).send();
  29. }
  30. }