ABSTRACT

This chapter introduces persisting data in a Flutter application. In this chapter, two approaches for persisting data on the disk permanently are explored. The previous chapter on Flutter themes discussed toggling application's themes from one to another. However, the selected theme setting was gone after next app's relaunch and required to switch theme again. It makes sense to persist the theme's selection from the last launch, so that users can have seamless experience across the multiple launches of the apps. In this chapter, you'll learn to persist theme selection using two approaches: Shared preferences and Local database. Shared preferences make better choices when there's a tiny amount of data that needs to be stored. Local database is a better choice for a huge dataset. In real-world applications, it makes sense to use the Shared preference approach to save theme selection. However, this chapter demonstrates both approaches to persist data for the selected theme. The Shared preference approach uses key/value pairs to store information on the device using Shared preference plugin. The Local database implementation uses moor library that is based on SQLite database. Persisting theme selection is demonstrated on all four platforms: Android, iOS, web, and desktop macOS apps.