Last active
December 6, 2022 18:21
-
-
Save fabiancrx/5ddf1f095157335ac14661937e29a385 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Interacts with services and provide observables that read or update settings. | |
/// | |
/// Controllers glue Data Services to Flutter Widgets. The SettingsController | |
/// uses the SettingsService to store and retrieve user settings. | |
class SettingsController with ChangeNotifier { | |
// Make SettingsService a private variable so it is not used directly. " Law of Demeter " | |
final SettingsService _settingsService; | |
late int counter; | |
SettingsController(this._settingsService); | |
/// Load the user's settings from the SettingsService. It may load from a | |
/// local database or the internet. The controller only knows it can load the | |
/// settings from the service. | |
Future<void> loadSettings() async { | |
counter = _settingsService.counter(); | |
//Inform listeners a change has occurred. | |
notifyListeners(); | |
} | |
/// Update and persist the counter to [newCount] | |
Future<void> updateCounter(int newCount) async { | |
//Increment the internal state of the counter | |
counter = newCount; | |
//Inform listeners a change has occurred. | |
notifyListeners(); | |
// Persist the changes | |
await _settingsService.updateCounter(newCount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment