<?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 Akanimoekong on Medium]]></title>
        <description><![CDATA[Stories by Akanimoekong on Medium]]></description>
        <link>https://medium.com/@akanimoekong?source=rss-1df3f4102016------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*m6XuOeHpZHSyigYIVbHpsA.jpeg</url>
            <title>Stories by Akanimoekong on Medium</title>
            <link>https://medium.com/@akanimoekong?source=rss-1df3f4102016------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 22:01:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@akanimoekong/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[Shared preference for Beginners]]></title>
            <link>https://medium.com/@akanimoekong/shared-preference-for-beginners-98f6030148c?source=rss-1df3f4102016------2</link>
            <guid isPermaLink="false">https://medium.com/p/98f6030148c</guid>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[flutter-app-development]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Akanimoekong]]></dc:creator>
            <pubDate>Wed, 31 May 2023 13:23:11 GMT</pubDate>
            <atom:updated>2023-06-02T12:29:35.591Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*_h6y6I6rlhBzkL04DYpHow.gif" /></figure><p>Coming from the native Side I have learned to leverage the power of shared Preference, shared preference has been helpful when it comes to storing small data like Username, email, password, etc.</p><p>Let&#39;s get started with this…</p><p><strong>What is Shared Preferences?</strong></p><p>Shared Preferences is used for storing or saving data key-value pairs in Android and iOS Devices.</p><p>SharedPreferences in flutter uses NSUserDefaults on iOS and SharedPreferences on Android, providing a persistent store for simple data.</p><p><strong>Why do you need SharedPreferences in Flutter?</strong></p><p>Suppose you want to save a small value (a UserId probably) that you want to reference later sometime when a user launches the application. Then shared preference stands a better chance here.</p><p>We do not use SQLite for saving small values because then you will need to write lengthy codes and supporting classes.</p><p><strong>How to use SharedPreferences in Flutter?</strong></p><p>Before using SharedPreferences, you should know that Flutter Package does not come with SharedPreferences but fortunately, the shared_preferences plugin can be used to persist key-value data.</p><p><strong>Step 1: Add the dependency<br> </strong>Add dependencies to pubspec.yaml file and Run pub get.</p><p>dependencies:<br> flutter:<br> sdk: flutter<br> shared_preferences: ^2.0.18</p><p><strong>Step 2: Import shared_preferences.dart<br> </strong>import ‘package:shared_preferences/shared_preferences.dart’;</p><p><strong>Step 3: Create a Helper class</strong></p><p>Create a Helper class that manages the SharedPreference(More like a Clean Code Experience)</p><pre>import &#39;package:shared_preferences/shared_preferences.dart&#39;;<br><br>class MyPreferences {<br>  //Initialized the SharedPreference<br>  static SharedPreferences? _preferences;<br><br>  // Set the Keys here so that you dont about setting the keys on all the screens,<br>  //Using this format &quot;static const keyname = value&quot;<br>  //&quot;static const keyname = value&quot;<br><br>  static const _keyfirstInput = &#39;username&#39;;<br>  static const _keysecondInput = &#39;email&#39;;<br><br><br>  // Function to initialise the SharedPreference which will be called at<br>// the void main of the project<br>  static Future init() async =&gt; _preferences = await SharedPreferences.getInstance();<br><br>  // Prepare the getter and the Setters for all the values that will be used i<br>  static Future setFirstInput(String firstInput) async =&gt;<br>      await _preferences!.setString(_keyfirstInput, firstInput);<br>  static String? getFirstInput() =&gt; _preferences!.getString(_keyfirstInput);<br><br>  static Future setSecondInput(String secondInput) async =&gt;<br>      await _preferences!.setString(_keysecondInput, secondInput);<br>  static String? getSecondInput() =&gt; _preferences!.getString(_keysecondInput);<br><br><br><br><br>}</pre><p><strong>Step 4: To store data in the SharedPreference</strong></p><p>use to the await MyPreferences.setUsername(inputText);</p><pre>    await MyPreferences.setFirstInput(firstInputController.text.trim());</pre><pre>                            Container(<br>                margin: EdgeInsets.only(top: 16, bottom: 8),<br>                child: TextFormField(<br>                  controller: firstInputController,<br>                  decoration: InputDecoration(<br>                    label: Text(<br>                      &#39;Enter first Input&#39;,<br>                    ),<br>                    border: OutlineInputBorder(<br>                      borderSide: BorderSide(<br>                        color: Colors.black38,<br>                      ),<br>                    ),<br>                  ),<br>                ),<br>              ),</pre><p>To Display the content of the Shared Preferecnce</p><p>Initialize the datatype of the data that you are about to Display</p><pre>String? firstInput;</pre><p>In your initState equate the sharedpreference to the already initialized variable.</p><pre>firstInput = MyPreferences.getFirstInput() ?? &#39;&#39;;</pre><p>Then bind the variable to the Text Widget</p><pre>Container(<br>  width: MediaQuery.of(context).size.width,<br>  height: MediaQuery.of(context).size.height / 10,<br>  decoration: BoxDecoration(<br>      borderRadius: BorderRadius.all(Radius.circular(8)),<br>      border: Border.all()),<br>  margin: EdgeInsets.only(top: 16, bottom: 8),<br>  child: Center(<br>    child: Text(<br>      &#39;Your first Input was: $firstInput&#39;,<br>    ),<br>  ),<br>),</pre><p>Your project is ready to go.</p><p>Need more information? See this… <a href="https://github.com/Akanimoekong/shared-preference-for-newbies">https://github.com/Akanimoekong/shared-preference-for-newbies</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=98f6030148c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>