Skip to content

Instantly share code, notes, and snippets.

@fabiancrx
Last active December 6, 2022 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabiancrx/5ddf1f095157335ac14661937e29a385 to your computer and use it in GitHub Desktop.
Save fabiancrx/5ddf1f095157335ac14661937e29a385 to your computer and use it in GitHub Desktop.
/// 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