PersistentContext constructor Null safety

  1. @override
  1. {Key? key,
  2. required Widget child,
  3. Map<String, dynamic> defaultValues = const {},
  4. String prefix = '',
  5. SharedPreferences? sharedPreferencesInstance}

Creates a widget that provides persistent data from shared preferences to all the descending widgets and allows to update it synchronously.

A defaultValues map can be provided to set the default values of the variables. A prefix string can also be provided, to be used for all the keys in shared preferences, for example to keep separated different contexts of your application.

The PersistentContext widget can be accessed from any child as an InheritedWidget, with the method PersistentContext.of(context).

If sharedPreferencesInstance is provided, the shared preferences will be imediately available. Otherwise they will be loaded asynchronously from storage only once, when the widget is created.

Any modification requested from the children will be notified immediately to all the descendants, and automatically consolidated to storage asynchronously.


  Key? key,
  required Widget child,
  this.defaultValues = const {},
  this.prefix = '',
  SharedPreferences? sharedPreferencesInstance,
}) : super(
        key: key,
        notifier: sharedPreferencesInstance != null
            ? PersistentContextNotifier.preloaded(sharedPreferencesInstance)
            : PersistentContextNotifier(),
        child: child,
      ) {
  defaultValues.forEach((key, value) {
    if (!(value is String ||
        value is int ||
        value is double ||
        value is bool)) {
      throw Exception("Invalid default preferences data structure. "
          "The value provided for key '$key' has type "
          "'${value.runtimeType}'. Only 'String', 'int', 'double' and 'bool' "
          "are allowed.");