|
| 1 | + |
| 2 | +Hyper Storage supports enums out of the box. Enums are a treated as special kind of data type as it is such data type |
| 3 | +in Dart language. |
| 4 | + |
| 5 | +> For any `Enum` related operations where a value needs to be read from underlying storage, you need to provide a |
| 6 | +> list of all possible enum values. This is due to limitations of Dart language and Flutter, which does not support |
| 7 | +> reflection at runtime (e.g. `Role.values`). |
| 8 | +
|
| 9 | +# Table of Contents |
| 10 | + |
| 11 | +- [Storing Enums](#storing-enums) |
| 12 | +- [Retrieving Enums](#retrieving-enums) |
| 13 | +- [Using ItemHolder with Enums](#using-itemholder-with-enums) |
| 14 | +- [Streaming Enums](#streaming-enums) |
| 15 | + |
| 16 | +## Storing Enums |
| 17 | + |
| 18 | +Storing enums is as simple as storing any other data type. You can store enums in a document like this: |
| 19 | + |
| 20 | +```dart |
| 21 | +await storage.setEnum('role', Role.guest); |
| 22 | +``` |
| 23 | +Under the hood, Hyper Storage stores enums as their string representation. For example, the enum value `Role.guest` |
| 24 | +is stored as the string `"guest"`. |
| 25 | + |
| 26 | +Storing enums in a container is also supported: |
| 27 | + |
| 28 | +```dart |
| 29 | +final container = await storage.container('settings'); |
| 30 | +await container.setEnum('brightness', Brightness.dark); |
| 31 | +``` |
| 32 | + |
| 33 | +You can also use the generic `set` method to store enums: |
| 34 | + |
| 35 | +```dart |
| 36 | +await storage.set('role', Role.admin); |
| 37 | +``` |
| 38 | + |
| 39 | + |
| 40 | +## Retrieving Enums |
| 41 | + |
| 42 | +Due to limitations of Dart, retrieving enums requires you to provide a list all possible enum values. |
| 43 | +This is necessary because Dart does not support reflection, and thus cannot determine the enum type at runtime. |
| 44 | + |
| 45 | +You can retrieve enums like this: |
| 46 | + |
| 47 | +```dart |
| 48 | +final role = await storage.getEnum<Role>('role', Role.values); |
| 49 | +``` |
| 50 | + |
| 51 | +If the stored value does not match any of the provided enum values, `null` is returned. The API for the container is |
| 52 | +also the same: |
| 53 | + |
| 54 | +```dart |
| 55 | +final container = await storage.container('settings'); |
| 56 | +final brightness = await container.getEnum<Brightness>('brightness', Brightness.values); |
| 57 | +``` |
| 58 | + |
| 59 | +You can also use the generic `get` method to retrieve enums: |
| 60 | + |
| 61 | +```dart |
| 62 | +final role = await storage.get<Role>('role', Role.values); |
| 63 | +``` |
| 64 | + |
| 65 | +## Using ItemHolder with Enums |
| 66 | + |
| 67 | +You can also use `ItemHolder` to store and retrieve enums. Here's an example: |
| 68 | + |
| 69 | +```dart |
| 70 | +final roleHolder = storage.itemHolder<Role>('role', Role.values); |
| 71 | +
|
| 72 | +// Set the enum value |
| 73 | +await roleHolder.set(Role.user); |
| 74 | +
|
| 75 | +// Get the enum value |
| 76 | +final role = await roleHolder.get(); |
| 77 | +``` |
| 78 | + |
| 79 | +## Streaming Enums |
| 80 | + |
| 81 | +You can stream changes to enum values using the `stream` method. Here's an example: |
| 82 | + |
| 83 | +```dart |
| 84 | +final roleStream = storage.stream<Role>('role', Role.values); |
| 85 | +
|
| 86 | +roleStream.listen((role) { |
| 87 | + print('Role changed to: $role'); |
| 88 | +}); |
| 89 | +``` |
| 90 | + |
| 91 | +With containers: |
| 92 | + |
| 93 | +```dart |
| 94 | +final container = await storage.container('settings'); |
| 95 | +
|
| 96 | +final brightnessStream = container.stream<Brightness>('brightness', Brightness.values); |
| 97 | +
|
| 98 | +brightnessStream.listen((brightness) { |
| 99 | + print('Brightness changed to: $brightness'); |
| 100 | +}); |
| 101 | +``` |
| 102 | + |
0 commit comments