{"id":414102,"date":"2022-03-09T17:46:05","date_gmt":"2022-03-09T14:46:05","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-use-sharedpreferences-to-store-app-settings-with-flutter-cloudsavvy-it\/"},"modified":"2022-03-09T17:46:05","modified_gmt":"2022-03-09T14:46:05","slug":"how-to-use-sharedpreferences-to-store-app-settings-with-flutter-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-use-sharedpreferences-to-store-app-settings-with-flutter-cloudsavvy-it\/","title":{"rendered":"#How to Use SharedPreferences to Store App Settings With Flutter \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2fd3fc853fc\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2fd3fc853fc\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-sharedpreferences-to-store-app-settings-with-flutter-cloudsavvy-it\/#%E2%80%9CHow_to_Use_SharedPreferences_to_Store_App_Settings_With_Flutter_%E2%80%93_CloudSavvy_IT%E2%80%9D\" >&#8220;How to Use SharedPreferences to Store App Settings With Flutter \u2013 CloudSavvy IT&#8221;<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-sharedpreferences-to-store-app-settings-with-flutter-cloudsavvy-it\/#Adding_SharedPreferences\" >Adding SharedPreferences<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-sharedpreferences-to-store-app-settings-with-flutter-cloudsavvy-it\/#Adding_Data_to_SharedPreferences\" >Adding Data to SharedPreferences<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-sharedpreferences-to-store-app-settings-with-flutter-cloudsavvy-it\/#Complex_Types\" >Complex Types<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-sharedpreferences-to-store-app-settings-with-flutter-cloudsavvy-it\/#Retrieving_Persisted_Data\" >Retrieving Persisted Data<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-sharedpreferences-to-store-app-settings-with-flutter-cloudsavvy-it\/#Caveats_and_Limitations\" >Caveats and Limitations<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-sharedpreferences-to-store-app-settings-with-flutter-cloudsavvy-it\/#Writing_Unit_Tests\" >Writing Unit Tests<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-sharedpreferences-to-store-app-settings-with-flutter-cloudsavvy-it\/#Summary\" >Summary<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E2%80%9CHow_to_Use_SharedPreferences_to_Store_App_Settings_With_Flutter_%E2%80%93_CloudSavvy_IT%E2%80%9D\"><\/span>&#8220;How to Use SharedPreferences to Store App Settings With Flutter \u2013 CloudSavvy IT&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage aligncenter size-full wp-image-14745\" data-pagespeed-lazy-srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/11\/af168a3a.jpeg?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/11\/af168a3a.jpeg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/11\/af168a3a.jpeg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Flutter logo\" width=\"1202\" height=\"677\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/pub.dev\/packages\/shared_preferences\">SharedPreferences<\/a> is a module bringing the functionality of Android\u2019s <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/developer.android.com\/training\/data-storage\/shared-preferences\">eponymous API<\/a> to your cross-platform Flutter <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>s. It uses Apple\u2019s similar NSUserDefaults API when running on iOS and macOS. Implementations are also included for Linux and Windows so it\u2019ll run everywhere Flutter apps can.<\/p>\n<p>In this guide, we\u2019ll show how you can use SharedPreferences to persist simple settings within your app. The module is best used for small pieces of non-essential state such as user preferences, saved bookmarks, and cached config values sent by a server.<\/p>\n<h2 id=\"adding-sharedpreferences\"><span class=\"ez-toc-section\" id=\"Adding_SharedPreferences\"><\/span>Adding SharedPreferences<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Add the SharedPreferences module to your app using the Flutter CLI\u2019s <code>pub add<\/code> command:<\/p>\n<pre>flutter pub add shared_preferences<\/pre>\n<p>Reference the library in your code by importing it at the top of your Dart files:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"dart\">\n<pre class=\"de1\"><span class=\"kw1\">import<\/span> <span class=\"st0\">'package:shared_preferences\/shared_preferences.dart'<\/span><span class=\"sy6\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>You\u2019ll be able to access the <code>SharedPreferences<\/code> API within files that include this <code>import<\/code> statement.<\/p>\n<h2 id=\"adding-data-to-sharedpreferences\"><span class=\"ez-toc-section\" id=\"Adding_Data_to_SharedPreferences\"><\/span>Adding Data to SharedPreferences<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To begin using SharedPreferences you must first get an instance of the <code>SharedPreferences<\/code> class that\u2019s connected to your app\u2019s on-disk config file. Call the <code>getInstance()<\/code> static method to load and parse the file into a <code>SharedPreferences<\/code> instance. This is an asynchronous method so use the <code>await<\/code> keyword inside an <code>async<\/code> function to streamline access.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"dart\">\n<pre class=\"de1\"><span class=\"kw2\">void<\/span> main<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span> async <span class=\"br0\">{<\/span>\n    <span class=\"kw1\">var<\/span> prefs <span class=\"sy3\">=<\/span> await SharedPreferences.<span class=\"me1\">getInstance<\/span><span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"sy6\">;<\/span>\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Now you can use the methods of the <code>prefs<\/code> instance to store and retrieve your data. SharedPreferences works with key-value pairs. It supports five data types: <code>int<\/code>, <code>String<\/code>, <code>bool<\/code>, <code>double<\/code>, and <code>List&lt;String&gt;<\/code>. Values are added to the store using setter methods; each method takes a key name as its first parameter and a value as its second:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"dart\">\n<pre class=\"de1\">prefs.<span class=\"me1\">setBool<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"darkTheme\"<\/span><span class=\"sy6\">,<\/span> <span class=\"kw1\">true<\/span><span class=\"br0\">)<\/span><span class=\"sy6\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>To work with other types of value, call their corresponding setter methods:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"dart\">\n<pre class=\"de1\">prefs.<span class=\"me1\">setString<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"theme\"<\/span><span class=\"sy6\">,<\/span> <span class=\"st0\">\"dark\"<\/span><span class=\"br0\">)<\/span><span class=\"sy6\">;<\/span>\n\u00a0\nprefs.<span class=\"me1\">setInt<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"sessionId\"<\/span><span class=\"sy6\">,<\/span> <span class=\"nu0\">1000<\/span><span class=\"br0\">)<\/span><span class=\"sy6\">;<\/span>\n\u00a0\nprefs.<span class=\"me1\">setDouble<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"lastTemperature\"<\/span><span class=\"sy6\">,<\/span> <span class=\"nu16\">18.5<\/span><span class=\"br0\">)<\/span><span class=\"sy6\">;<\/span>\n\u00a0\nprefs.<span class=\"me1\">setStringList<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"featureFlags\"<\/span><span class=\"sy6\">,<\/span> <span class=\"br0\">[<\/span><span class=\"st0\">\"darkTheme\"<\/span><span class=\"sy6\">,<\/span> <span class=\"st0\">\"redesign\"<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><span class=\"sy6\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The setters don\u2019t accept <code>null<\/code> as a value. Use the separate <code>remove()<\/code> method to delete an item from the store:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"dart\">\n<pre class=\"de1\">prefs.<span class=\"me1\">remove<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"sessionId\"<\/span><span class=\"br0\">)<\/span><span class=\"sy6\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>To delete all the stored settings, call the <code>clear()<\/code> method. Clears are asynchronous operations which resolve when the on-disk settings file has been removed.<\/p>\n<h3 id=\"complex-types\"><span class=\"ez-toc-section\" id=\"Complex_Types\"><\/span>Complex Types<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>You can\u2019t natively persist complex types such as objects, maps, and non-string lists. To store one of these, you could encode it as JSON and use the <code>setString()<\/code> method:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"dart\">\n<pre class=\"de1\"><span class=\"kw1\">import<\/span> <span class=\"st0\">'dart:convert'<\/span><span class=\"sy6\">;<\/span>\n<span class=\"kw1\">import<\/span> <span class=\"st0\">'package:shared_preferences\/shared_preferences.dart'<\/span><span class=\"sy6\">;<\/span>\n\u00a0\n<span class=\"kw2\">void<\/span> main <span class=\"br0\">(<\/span><span class=\"br0\">)<\/span> async <span class=\"br0\">{<\/span>\n    <span class=\"kw1\">var<\/span> bookmarkedArticles <span class=\"sy3\">=<\/span> <span class=\"br0\">[<\/span><span class=\"nu0\">10<\/span><span class=\"sy6\">,<\/span> <span class=\"nu0\">15<\/span><span class=\"sy6\">,<\/span> <span class=\"nu0\">18<\/span><span class=\"br0\">]<\/span><span class=\"sy6\">;<\/span>\n    <span class=\"kw1\">var<\/span> prefs <span class=\"sy3\">=<\/span> await SharedPreferences.<span class=\"me1\">getInstance<\/span><span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"sy6\">;<\/span>\n    prefs.<span class=\"me1\">setString<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"bookmarks\"<\/span><span class=\"sy6\">,<\/span> jsonEncode<span class=\"br0\">(<\/span>bookmarkedArticles<span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><span class=\"sy6\">;<\/span>\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Call <code>jsonDecode()<\/code> when you retrieve the value with <code>getString()<\/code> (see below) to convert it back to an appropriate Dart type.<\/p>\n<h2 id=\"retrieving-persisted-data\"><span class=\"ez-toc-section\" id=\"Retrieving_Persisted_Data\"><\/span>Retrieving Persisted Data<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Values are retrieved using getter methods. Like with the setters, there\u2019s a unique getter for each supported data type. This means you can safely use the return values without having to manually cast between types.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"dart\">\n<pre class=\"de1\"><span class=\"kw1\">var<\/span> theme <span class=\"sy3\">=<\/span> prefs.<span class=\"me1\">getString<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"theme\"<\/span><span class=\"br0\">)<\/span><span class=\"sy6\">;<\/span>\n<span class=\"co0\">\/\/ \"dark\"<\/span>\n\u00a0\n<span class=\"kw1\">var<\/span> sessionId <span class=\"sy3\">=<\/span> prefs.<span class=\"me1\">getInt<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"sessionId\"<\/span><span class=\"br0\">)<\/span><span class=\"sy6\">;<\/span>\n<span class=\"co0\">\/\/ 1000<\/span>\n\u00a0\n<span class=\"kw1\">var<\/span> lastTemperature <span class=\"sy3\">=<\/span> prefs.<span class=\"me1\">getDouble<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"lastTemperature\"<\/span><span class=\"br0\">)<\/span><span class=\"sy6\">;<\/span>\n<span class=\"co0\">\/\/ 18.5<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>It\u2019s important you use the getter that matches the data type of the stored value. If the key matches but it has a different data type, an exception will be thrown.<\/p>\n<p>Null is returned when the key doesn\u2019t exist in the saved data. You can check whether an item\u2019s in the store in advance by calling <code>containsKey()<\/code>:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"dart\">\n<pre class=\"de1\"><span class=\"kw1\">if<\/span> <span class=\"br0\">(<\/span>prefs.<span class=\"me1\">containsKey<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"theme\"<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span>\n    <span class=\"co0\">\/\/ ...<\/span>\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>You can also enumerate all the keys in the store:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"dart\">\n<pre class=\"de1\"><span class=\"kw1\">var<\/span> keys <span class=\"sy3\">=<\/span> pref.<span class=\"me1\">getKeys<\/span><span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"sy6\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>This is useful when you need to know what\u2019s currently saved on-disk so you can run appropriate migrations after an app update.<\/p>\n<h2 id=\"caveats-and-limitations\"><span class=\"ez-toc-section\" id=\"Caveats_and_Limitations\"><\/span>Caveats and Limitations<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>SharedPreferences is easy to work with when you\u2019re storing simple key-value pairs. However you shouldn\u2019t rely on it for critical data or security-sensitive values.<\/p>\n<p>Platform-specific implementations vary but you should assume your data will be stored as plain-text in a potentially user-accessible location. Encrypting values before you save them can add more protection but will still leave the keys exposed.<\/p>\n<p>SharedPreferences is an asynchronous API. While the getter and setter methods look like they\u2019re synchronous, that doesn\u2019t extend all the way down to the disk level. Disk writes <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/pub.dev\/packages\/shared_preferences\">may be asynchronous<\/a> so closing the app im<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>tely after setting a value could cause it to be lost.<\/p>\n<p>It\u2019s best to choose a different API when you\u2019re storing large amounts of data, writing complex values, or in need of a more dependable operating model with resilient error handling. You could use the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.flutter.dev\/cookbook\/persistence\/reading-writing-files\">Files API<\/a> to write a custom config file to a restricted location or create a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.flutter.dev\/cookbook\/persistence\/sqlite\">SQLite database<\/a> for your app. The latter approach is ideal when you\u2019ll want to perform advanced queries against the stored data.<\/p>\n<h2 id=\"writing-unit-tests\"><span class=\"ez-toc-section\" id=\"Writing_Unit_Tests\"><\/span>Writing Unit Tests<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>SharedPreferences includes a utility to help you write unit tests without a real preferences store. The static <code>setMockInitialValues()<\/code> method lets you inject values that will populate the instance returned by <code>getInstance()<\/code>:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"dart\">\n<pre class=\"de1\"><span class=\"kw2\">void<\/span> main<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span> async <span class=\"br0\">{<\/span>\n\u00a0\n    Map<span class=\"sy3\">&lt;<\/span>String<span class=\"sy6\">,<\/span> Object<span class=\"sy3\">&gt;<\/span> values <span class=\"sy3\">=<\/span> <span class=\"sy3\">&lt;<\/span>String<span class=\"sy6\">,<\/span> Object<span class=\"sy3\">&gt;<\/span><span class=\"br0\">{<\/span><span class=\"st0\">\"foo\"<\/span><span class=\"sy4\">:<\/span> <span class=\"st0\">\"bar\"<\/span><span class=\"br0\">}<\/span><span class=\"sy6\">;<\/span>\n    SharedPreferences.<span class=\"me1\">setMockInitialValues<\/span><span class=\"br0\">(<\/span>values<span class=\"br0\">)<\/span><span class=\"sy6\">;<\/span>\n\u00a0\n    <span class=\"kw1\">var<\/span> instance <span class=\"sy3\">=<\/span> await SharedPreferences.<span class=\"me1\">getInstance<\/span><span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"sy6\">;<\/span>\n    <span class=\"kw1\">var<\/span> foo <span class=\"sy3\">=<\/span> instance.<span class=\"me1\">getString<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"foo\"<\/span><span class=\"br0\">)<\/span><span class=\"sy6\">;<\/span>\n    <span class=\"co0\">\/\/ bar<\/span>\n\u00a0\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>You can add settings to your app while maintaining your ability to unit test each component. Testing the code that retrieves your settings lets you guarantee the app will behave correctly in each scenario.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>SharedPreferences is a convenient way to persist simple state inside your Flutter app. It focuses on the storage and retrieval of key-value pairs using the most common data types. SharedPreferences is best used for non-essential settings where a failed write won\u2019t negatively impact your application.<\/p>\n<p>It can be effective to leverage SharedPreferences in a similar manner to cookies in web applications. Basic counters, boolean values, and config keys make sense in SharedPreferences; everything else belongs in a well-defined data structure that you can manage more precisely.\n<\/p><\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/15377\/how-to-use-sharedpreferences-to-store-app-settings-with-flutter\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;How to Use SharedPreferences to Store App Settings With Flutter \u2013 CloudSavvy IT&#8221; SharedPreferences is a module bringing the functionality of Android\u2019s eponymous API to your cross-platform Flutter apps. It uses Apple\u2019s similar NSUserDefaults API when running on iOS and macOS. Implementations are also included for Linux and Windows so it\u2019ll run everywhere Flutter apps&#8230;<\/p>\n","protected":false},"author":1,"featured_media":414103,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/11\/af168a3a.jpeg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-414102","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/414102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=414102"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/414102\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/414103"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=414102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=414102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=414102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}