https://github.com/roughike/multiplatform_key_value_store
https://github.com/roughike/multiplatform_key_value_store
Last synced: 9 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/roughike/multiplatform_key_value_store
- Owner: roughike
- License: bsd-2-clause
- Created: 2018-10-08T10:15:49.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-11-13T08:46:58.000Z (about 6 years ago)
- Last Synced: 2025-04-14T16:21:53.854Z (9 months ago)
- Language: Dart
- Size: 43 KB
- Stars: 29
- Watchers: 2
- Forks: 10
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Key-value store for multiplatform Dart projects
> What the heck is that?
Glad you asked! (docs are work in progress, but the libraries are stable.)
Multiplatform key-value store is like any other key value store, but it's _multiplatform_. Badum-tss!
# Package overview
This repo contains three different folders. Each of them is a Dart package project.
## key_value_store [](https://pub.dartlang.org/packages/key_value_store)
The `key_value_store` package defines common key-value storage APIs in an abstract way without caring about the implementation.
You might ask what is the point for this, and that is an entirely valid question!
When you're doing code sharing across Flutter and the web, you can't import platform specific dependencies in your core business logic components.
Using `localStorage` for web or `SharedPreferences` for Flutter in your pure business logic is a no-no.
Here's how you would use the abstract class in your common business logic:
```dart
import 'package:key_value_store/key_value_store.dart';
class MyBusinessLogic {
MyBusinessLogic(this.kvs);
final KeyValueStore kvs;
void storeHelloMessage(String name) {
final result = 1 + 2; // Real world is more complicated - this is just a sample.
kvs.setString('message', 'Hello, $name! Did you know that 1 + 2 is $result?');
}
}
```
## key_value_store_flutter [](https://pub.dartlang.org/packages/key_value_store_flutter)
This implements the abstract class defined in `key_value_store` with Flutter-specific implementation.
In this case, using `SharedPreferences`.
To use, pass it `SharedPreferences` from the [shared_preferences](https://pub.dartlang.org/packages/shared_preferences) Flutter plugin package:
```dart
import 'package:key_value_store_flutter/key_value_store_flutter.dart';
import 'package:shared_preferences/shared_preferences.dart';
final prefs = await SharedPreferences.getInstance();
final kvs = FlutterKeyValueStore(prefs);
...
// Pass the Flutter specific key-value store to MyBusinessLogic!
final myBusinessLogic = MyBusinessLogic(kvs);
```
## key_value_store_web [](https://pub.dartlang.org/packages/key_value_store_web)
This is also an implementation of the interface defined in the `key_value_store` package.
Pass it `window.localStorage` or `window.sessionStorage` from the `dart:html` package and you're good to go:
```dart
import 'package:key_value_store_web/key_value_store_web.dart';
import 'dart:html';
final kvs = WebKeyValueStore(window.localStorage);
...
// Pass the web specific key-value store to MyBusinessLogic!
final myBusinessLogic = MyBusinessLogic(kvs);
```