Better persisting

This commit is contained in:
Vicente J. Ruiz Jurado 2018-08-03 08:45:56 +02:00
parent c39351d0df
commit 04cc6fef83
2 changed files with 16 additions and 12 deletions

View file

@ -2,7 +2,7 @@ import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'package:fires_flutter/models/fireNotification.dart'; import 'package:fires_flutter/models/fireNotification.dart';
import 'package:comunes_flutter/comunes_flutter.dart';
import '../globals.dart' as globals; import '../globals.dart' as globals;
final String fireNotificationKey = 'fireNotifications'; final String fireNotificationKey = 'fireNotifications';
@ -16,10 +16,12 @@ Future<List<FireNotification>> loadFireNotifications() async {
// first run, init with empty list // first run, init with empty list
persistFireNotifications(persistedList); persistFireNotifications(persistedList);
} }
FireNotifications.forEach((notificationString) { if (FireNotifications is List) {
Map notificationMap = json.decode(notificationString); FireNotifications.forEach((notificationString) {
persistedList.add(FireNotification.fromJson(notificationMap)); Map notificationMap = json.decode(notificationString);
}); persistedList.add(FireNotification.fromJson(notificationMap));
});
}
return persistedList; return persistedList;
}); });
} }
@ -28,7 +30,7 @@ persistFireNotifications(List<FireNotification> notif) {
print('Persisting $notif'); print('Persisting $notif');
globals.prefs.then((prefs) { globals.prefs.then((prefs) {
List<String> notifAsString = []; List<String> notifAsString = [];
notif.forEach((notification) { notif.where(notNull).toList().forEach((notification) {
notifAsString.add(json.encode(notification.toJson())); notifAsString.add(json.encode(notification.toJson()));
}); });
prefs.setStringList(fireNotificationKey, notifAsString); prefs.setStringList(fireNotificationKey, notifAsString);

View file

@ -2,7 +2,7 @@ import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'package:fires_flutter/models/yourLocation.dart'; import 'package:fires_flutter/models/yourLocation.dart';
import 'package:comunes_flutter/comunes_flutter.dart';
import '../globals.dart' as globals; import '../globals.dart' as globals;
final String locationKey = 'yourlocations'; final String locationKey = 'yourlocations';
@ -16,10 +16,12 @@ Future<List<YourLocation>> loadYourLocations() async {
// first run, init with empty list // first run, init with empty list
persistYourLocations(persistedList); persistYourLocations(persistedList);
} }
yourLocations.forEach((locationString) { if (yourLocations is List) {
Map locationMap = json.decode(locationString); yourLocations.forEach((locationString) {
persistedList.add(YourLocation.fromJson(locationMap)); Map locationMap = json.decode(locationString);
}); persistedList.add(YourLocation.fromJson(locationMap));
});
}
return persistedList; return persistedList;
}); });
} }
@ -28,7 +30,7 @@ persistYourLocations(List<YourLocation> yl) {
print('Persisting $yl'); print('Persisting $yl');
globals.prefs.then((prefs) { globals.prefs.then((prefs) {
List<String> ylAsString = []; List<String> ylAsString = [];
yl.forEach((location) { yl.where(notNull).toList().forEach((location) {
ylAsString.add(json.encode(location.toJson())); ylAsString.add(json.encode(location.toJson()));
}); });
prefs.setStringList(locationKey, ylAsString); prefs.setStringList(locationKey, ylAsString);