Source codeClick here to view the full source code of this example
SharedPreferences is a package that allows you to store key-value pairs on the device.
This example shows how to provide a SharedPreferences object, after initialization, to the whole app using Disco.
After the initialization, the preferences can be retrieved and updated synchronously.
You can take inspiration to provide other asynchronous objects.
Dependency
This is the shared_preferences version used in the following example:
Example
The code below handles the loading and error states of the initialization process correctly and it’s the preferred way for handling async initialization in Flutter.
Alternative example (without error handling)
If you know the initialization cannot fail, or it takes just a fraction of a second and you don’t want to show the loading state, you can simplify the code as follows: