todos-contra-el-fuego-mobile/lib/yourLocationPersist.dart
2018-06-27 12:08:36 +02:00

36 lines
1 KiB
Dart

import 'dart:convert';
import 'package:fires_flutter/models/yourLocation.dart';
import 'globals.dart' as globals;
import 'package:shared_preferences/shared_preferences.dart';
final String locationKey = 'yourlocations';
void loadYourLocations() {
globals.prefs.then((prefs) {
loadYourLocationsWithPrefs(prefs);
});
}
persistYourLocations() {
globals.prefs.then((prefs) {
List<String> yourLocationsAsString = [];
globals.yourLocations.forEach((location) {
yourLocationsAsString.add(json.encode(location.toJson()));
});
prefs.setStringList(locationKey, yourLocationsAsString);
});
}
void loadYourLocationsWithPrefs(SharedPreferences prefs) {
globals.prefs.then((prefs) {
List<String> yourLocations = prefs.getStringList(locationKey);
if (yourLocations == null) {
yourLocations = [];
persistYourLocations();
}
yourLocations.forEach((locationString) {
Map locationMap = json.decode(locationString);
globals.yourLocations.add(YourLocation.fromJson(locationMap));
});
});
}