pref_dessert 0.3.0

pref_dessert #

pub package Build Status

Package that allows you persist objects as shared preferences easily. Package name comes from: Shared PREFerences DESerializer/SERializer of T (generic) class.

Getting Started #

To use this package you just need to extend DesSer<T> class, replace generic T class with the one you want to save in shared preferences. This class requires you to implement two methods to serialize and deserialize objects. In future releases I'm planning to use JSON serializer so it will be much simpler to use. Then pass this class to PreferenceRepository or FuturePreferencesRepository and you're good to go!

Example #

Person class that you want to serialize:


class Person {
  String name;
  int age;

  Person(this.name, this.age);
}

PersonDesSer which extends DesSer<T> and implements two methods which serialize this objects using CSV format:


class PersonDesSer extends DesSer<Person>{
  @override
  Person deserialize(String s) {
    var split = s.split(",");
    return new Person(split[0], int.parse(split[1]));
  }

  @override
  String serialize(Person t) {
    return "${t.name},${t.age}";
  }
  
}

You can also do this using JSON with convert package:

import 'dart:convert';

class JsonPersonDesSer extends DesSer<Person>{
  @override
  Person deserialize(String s) {
    var map = json.decode(s);
    return new Person(map['name'] as String, map['age'] as int);
  }

  @override
  String serialize(Person t) {
    var map = {"name":t.name, "age":t.age};
    return json.encode(map);
  }

}

Then create an instance of either class and pass it to the FuturePreferencesRepository<T>:

var repo = new FuturePreferencesRepository<Person>(new PersonDesSer());
repo.save(new Person("FooBar", 42));
var list = repo.findAll();

0.3.0 #

  • shared_preferences dependency update

0.2.3 #

  • test version update

0.2.2 #

  • Dart and shared_preferences version update

0.2.1 #

  • added SingleElementPreferencesRepository is simplified version of PreferencesRepository which only stores one value

0.1.0 #

  • added removeWhere method

0.0.10 #

  • source code reformatted

0.0.9 #

  • added saveAll method

0.0.8 #

  • fixed bug with shared preferences key

0.0.7 #

  • update method bug-fix

0.0.6 #

  • added example and more comments in code

0.0.5 #

  • tests bug fix

0.0.4 #

  • There are now two repository classes - 'normal' and Future version
  • Added more methods to repository classes
  • DesSer<T> class for DESserializing and SERializing classes instead of two abstract methods in PreferencesRepository

0.0.2 #

  • Better description with example
  • Test for class serialization with json.

0.0.1 #

  • First version

example/main.dart

import 'package:pref_dessert/pref_dessert.dart';

/// Person class that you want to serialize:
class Person {
  String name;
  int age;

  Person(this.name, this.age);
}

/// PersonDesSer which extends DesSer<T> and implements two methods which serialize this objects using CSV format:
class PersonDesSer extends DesSer<Person>{
  @override
  Person deserialize(String s) {
    var split = s.split(",");
    return new Person(split[0], int.parse(split[1]));
  }

  @override
  String serialize(Person t) {
    return "${t.name},${t.age}";
  }

  @override
  String get key => "Person";

}

void main() {
  var repo = new FuturePreferencesRepository<Person>(new PersonDesSer());
  repo.save(new Person("Foo", 42));
  repo.save(new Person("Bar", 1));
  var list = repo.findAll();

}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  pref_dessert: ^0.3.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:pref_dessert/pref_dessert.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
67
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
83
Learn more about scoring.

We analyzed this package on Jul 17, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19
  • Flutter: 1.7.8+hotfix.3

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/pref_dessert.dart.

Run flutter format to format lib/pref_dessert.dart.

Format lib/pref_dessert_internal.dart.

Run flutter format to format lib/pref_dessert_internal.dart.

Format lib/src/repository.dart.

Run flutter format to format lib/src/repository.dart.

Format lib/src/single_element.dart.

Run flutter format to format lib/src/single_element.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.0 <3.0.0
flutter 0.0.0
json_annotation ^2.2.0 2.4.0
shared_preferences >=0.5.3 <1.0.0 0.5.3+4
Transitive dependencies
collection 1.14.11
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
build_runner ^1.3.3
flutter_driver
flutter_test
test ^1.5.2
test_api ^0.2.4

Admin