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