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