<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Raghul on Medium]]></title>
        <description><![CDATA[Stories by Raghul on Medium]]></description>
        <link>https://medium.com/@raghul17m?source=rss-b468dd89fd98------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ZngmHqniOcr-f30SVuGlnw.png</url>
            <title>Stories by Raghul on Medium</title>
            <link>https://medium.com/@raghul17m?source=rss-b468dd89fd98------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 23:14:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@raghul17m/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Flutter Shared Preferences]]></title>
            <link>https://medium.com/@raghul17m/flutter-shared-preferences-4f6da336e36b?source=rss-b468dd89fd98------2</link>
            <guid isPermaLink="false">https://medium.com/p/4f6da336e36b</guid>
            <category><![CDATA[localstorage]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[flutter-web]]></category>
            <category><![CDATA[flutter-app-development]]></category>
            <dc:creator><![CDATA[Raghul]]></dc:creator>
            <pubDate>Fri, 20 Mar 2026 11:45:37 GMT</pubDate>
            <atom:updated>2026-03-24T07:51:36.579Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*g1lHiQ3MDcqlXVCj_cQD7w.png" /></figure><p>In this article, we are going to discuss <strong>SharedPreferences</strong> in Flutter. In this article, we will cover:</p><p><em>What is SharedPreferences?</em></p><p><em>Why use SharedPreferences over other options?</em></p><p><em>How SharedPreferences works ?</em></p><p><em>When to use it in your project?</em></p><p><em>How to integrate SharedPreferences in a Flutter app?</em></p><p>Let’s dive in!</p><h3>What is SharedPreferences?</h3><p>SharedPreferences is a <strong>local storage mechanism</strong> in Flutter that allows you to store data on the device. It stores data in the form of <strong>key-value pairs</strong>.</p><p>It is commonly used for storing simple app data such as:</p><ul><li>User login status</li><li>Theme preference (dark/light mode)</li><li>Language settings</li></ul><p>In short, it’s perfect for small amounts of data that need to persist between app launches.</p><h3>Why Use SharedPreferences?</h3><p>Even though there are many storage plugins on <a href="https://pub.dev/">pub.dev</a>, SharedPreferences remains widely used in Flutter because it is:</p><ul><li><strong>Simple</strong> — Easy to store and retrieve data using key-value pairs</li><li><strong>Lightweight</strong> — Minimal setup and low resource usage</li><li><strong>Fast</strong> — Ideal for small data compared to full-fledged databases</li><li><strong>Beginner-friendly</strong> — No complex queries or schemas needed</li></ul><p>So, if your app only needs to store small pieces of data, SharedPreferences is the most convenient option.</p><h3>Supported Data Types</h3><p>SharedPreferences can store <strong>primitive data types</strong>:</p><ul><li>String</li><li>int</li><li>double</li><li>bool</li><li>List&lt;String&gt;</li></ul><p>It <strong>does not support complex objects directly</strong>, but we can handle that by converting objects to JSON strings.</p><h3>How to Integrate SharedPreferences in a Flutter Project</h3><p>Using SharedPreferences in Flutter is straightforward. Here’s how you can integrate it step by step.</p><p>Step 1: Add the Dependency</p><p>Open your pubspec.yaml file and add:</p><p>dependencies:<br> flutter:<br> sdk: flutter<br> shared_preferences: ^2.1.0 # check for the latest version on pub.dev</p><p>Then run:</p><p>flutter pub get</p><p>Step 2: Create an Instance</p><p>You should create a single instance of SharedPreferences and reuse it across the app:</p><p>late SharedPreferences prefs;</p><p>void main() async {<br> WidgetsFlutterBinding.ensureInitialized();<br> prefs = await SharedPreferences.getInstance();<br> runApp(MyApp());<br>}</p><p>This approach avoids creating multiple instances and keeps your code clean.</p><p>Step 3: Add Data</p><p>You can store different types of data like this:</p><p>void addData() async {<br> await prefs.setString(‘username’, ‘John’);<br> await prefs.setInt(‘age’, 25);<br> await prefs.setBool(‘isLoggedIn’, true);<br> await prefs.setDouble(‘height’, 5.9);<br> await prefs.setStringList(‘fruits’, [‘apple’, ‘banana’, ‘mango’]);<br> <br> print(“Data saved successfully!”);<br>}<br>Storing Complex Objects</p><p>SharedPreferences cannot store Maps directly, but you can store them as JSON strings:</p><p>Map&lt;String, dynamic&gt; user = {<br> ‘name’: ‘John’,<br> ‘age’: 25,<br> ‘isLoggedIn’: true<br>};</p><p>// Convert Map to JSON string<br>String userJson = jsonEncode(user);</p><p>// Save JSON string<br>await prefs.setString(‘userDetails’, userJson);</p><p>Step 4: Retrieve Data</p><p>Retrieving data is just as easy:</p><p>void getData() async {<br> String? username = prefs.getString(‘username’);<br> int? age = prefs.getInt(‘age’);<br> bool? isLoggedIn = prefs.getBool(‘isLoggedIn’);<br> double? height = prefs.getDouble(‘height’);<br> List&lt;String&gt;? fruits = prefs.getStringList(‘fruits’);</p><p>print(‘Username: $username’);<br> print(‘Age: $age’);<br> print(‘Logged in: $isLoggedIn’);<br> print(‘Height: $height’);<br> print(‘Fruits: $fruits’);<br>}<br>Retrieving Complex Objects</p><p>If you stored a Map as a JSON string, decode it like this:</p><p>String? userJson = prefs.getString(‘userDetails’);</p><p>if (userJson != null) {<br> Map&lt;String, dynamic&gt; userMap = jsonDecode(userJson);<br> print(userMap);<br>}</p><h3>How SharedPreferences Works Behind the Scenes</h3><p>Understanding how it works in the background gives you an edge:</p><ul><li><strong>Android:</strong> Stores data in XML files in the app’s internal storage</li><li><strong>iOS:</strong> Uses NSUserDefaults (Apple’s built-in key-value storage)</li><li><strong>Web:</strong> Uses window.localStorage in the browser</li></ul><p>This ensures that data persists even if the app is closed or the device restarts. Importantly, the data is <strong>private to your app</strong>.</p><h3>When to Use SharedPreferences</h3><p>Use SharedPreferences when you need to store <strong>small amounts of simple data</strong>:</p><ul><li>User preferences</li><li>App settings</li><li>Login session data</li></ul><p>For <strong>large or relational data</strong>, consider using databases like <strong>SQLite</strong> or <strong>Hive</strong>.</p><p>SharedPreferences is a simple yet powerful tool for Flutter developers. Mastering it will help you manage persistent app data efficiently.</p><p><strong>Follow me for more advanced Flutter tutorials and tips to level up your Flutter skills!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4f6da336e36b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>