All Projects → roughike → multiplatform_key_value_store

roughike / multiplatform_key_value_store

Licence: BSD-2-Clause license
No description, website, or topics provided.

Programming Languages

dart
5743 projects
java
68154 projects - #9 most used programming language
objective c
16641 projects - #2 most used programming language
ruby
36898 projects - #4 most used programming language
shell
77523 projects

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 pub package

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:

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 pub package

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 Flutter plugin package:

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 pub package

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:

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);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].