Package that allows you persist objects as shared preferences easily. Package name comes from: Shared PREFerences DESerializer/SERializer of T (generic) class.
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!
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();
test
version updateDart
and shared_preferences
version updateremoveWhere
methodsaveAll
method update
method bug-fixFuture
versionDesSer<T>
class for DESserializing and SERializing classes instead of two abstract methods in PreferencesRepository
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();
}
Add this to your package's pubspec.yaml file:
dependencies:
pref_dessert: ^0.2.3
You can install packages from the command line:
with Flutter:
$ flutter packages get
Alternatively, your editor might support flutter packages get
.
Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:pref_dessert/pref_dessert.dart';
Version | Uploaded | Documentation | Archive |
---|---|---|---|
0.2.3 | Oct 18, 2018 |
|
|
0.2.2 | Oct 16, 2018 |
|
|
0.2.1 | Sep 11, 2018 |
|
|
0.2.0 | Sep 11, 2018 |
|
|
0.1.0 | Sep 8, 2018 |
|
|
0.0.10 | Sep 5, 2018 |
|
|
0.0.9 | Sep 5, 2018 |
|
|
0.0.8 | Jul 22, 2018 |
|
|
0.0.7 | Jul 22, 2018 |
|
|
0.0.6 | Jul 16, 2018 |
|
|
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
72
|
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]
|
86
|
We analyzed this package on Feb 20, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
Detected platforms: Flutter
References Flutter, and has no conflicting libraries.
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
.
Package | Constraint | Resolved | Available |
---|---|---|---|
Direct dependencies | |||
Dart SDK | >=2.0.0 <3.0.0 | ||
flutter | 0.0.0 | ||
json_annotation | ^1.1.0 | 1.2.0 | 2.0.0 |
shared_preferences | ^0.4.3 | 0.4.3 | 0.5.1+1 |
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 | 0.10.1+1 | ||
flutter_driver | |||
flutter_test | |||
json_serializable | 1.2.1 | ||
test | ^1.3.0 |