<?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 Vaibhav Miniyar on Medium]]></title>
        <description><![CDATA[Stories by Vaibhav Miniyar on Medium]]></description>
        <link>https://medium.com/@vaibhavminiyar?source=rss-a31a79a2da98------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*LwtFOf778E1ipi2kbgRxcw.jpeg</url>
            <title>Stories by Vaibhav Miniyar on Medium</title>
            <link>https://medium.com/@vaibhavminiyar?source=rss-a31a79a2da98------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 22:33:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vaibhavminiyar/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[Implementing FirebaseAuthStateListener using Shared Prefrences in Flutter.]]></title>
            <link>https://medium.com/hackernoon/implementing-firebaseauthstatelistener-using-shared-prefrences-in-flutter-b42e12f81eb2?source=rss-a31a79a2da98------2</link>
            <guid isPermaLink="false">https://medium.com/p/b42e12f81eb2</guid>
            <category><![CDATA[firebaseauthentication]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[firebase]]></category>
            <dc:creator><![CDATA[Vaibhav Miniyar]]></dc:creator>
            <pubDate>Sat, 26 May 2018 15:20:51 GMT</pubDate>
            <atom:updated>2019-05-21T08:32:58.378Z</atom:updated>
            <content:encoded><![CDATA[<p>Hello ✌️,</p><p>I started Flutter a month ago. I have built few small application in Flutter like <a href="https://github.com/im-minion/Flutter-Chat-App">Chat Application</a> , <a href="https://github.com/im-minion/Flutter-Blog-App-Firebase">Blog Application</a> etc. I used <a href="https://hackernoon.com/tagged/firebase">Firebase</a> for both the apps mentioned. One thing that I have realized while making blog application is about firebase auth state listener. In the flutter+firebase I didn’t find the Firebase Auth State listener as like in <a href="https://hackernoon.com/tagged/android">Android</a>. (Auth state listener checks and continuously monitors the status of existence of the user and respectively shows the screen or activity in terms of Android)</p><p>So in order get the current status of user and show the respective Scaffold or Material App or any respective component in Flutter, I used <a href="https://pub.dartlang.org/packages/shared_preferences">“Shared Prefrences”</a> library in Flutter</p><p>I have used Google Authenticatoin currently eith SharedPrefernce but one can use any of the Auth Method you want.</p><p>In <a href="https://github.com/im-minion/Flutter-Blog-App-Firebase">Flutter Blog App</a> I have Declared 2 Scaffolds viz. loginScaffold and homeScaffold. and Changed them with respect to Auth State.<br>If user already Logged in then showing homeScaffold else showing loginScaffold. After that if Login happens successfully loginScaffold will be replaced by the homeScaffold.</p><p>Steps touse Shared Preference for Auth State Listening:</p><h4>1. Adding Library to pubspec.yaml and importing in HomePage.dart</h4><pre><strong>dependencies:<br>  flutter:<br>    sdk: </strong>flutter</pre><pre>  <strong>cupertino_icons: </strong>^0.1.0<br>  <strong>shared_preferences: &quot;^0.4.0&quot;<br>  .<br>  .<br>  .</strong></pre><p>Then importing into HomePage.dart as</p><pre>import ‘package:shared_preferences/shared_preferences.dart’;</pre><h4>2. Create Instance of Shared Prefernce and checking Existance of value in same :</h4><p>For the main screen I have used a material app whose “home :” attribute depends on current status of user that means if user is logged in it will show the homeScaffold and if not then will show loginScaffold.<br>As Follows: (Code is Self Explantory :) )</p><pre><strong>class </strong>HomePage <strong>extends </strong>StatefulWidget {<br>  @override<br>  HomePageState createState() =&gt; <strong>new </strong>HomePageState();<br>}<br><br><strong>class </strong>HomePageState <strong>extends </strong>State&lt;HomePage&gt; {<br>  bool <strong>loggedIn </strong>= <strong>false</strong>;  // this will check status</pre><pre>Future&lt;Null&gt; _function() <strong>async </strong>{<br>  <br>    /**<br>    This Function will be called every single time<br>    when application is opened and it will check <br>    if the value inside Shared Preference exist or not<br>    **/</pre><pre>    SharedPreferences prefs;<br>    prefs = <strong>await </strong>SharedPreferences.<em>getInstance</em>();<br>    <strong>this</strong>.setState(() {<br>      <strong>if </strong>(prefs.getString(<strong>&quot;username&quot;</strong>) != <strong>null</strong>) {<br>        <strong>loggedIn </strong>= <strong>true</strong>;<br>      } <strong>else </strong>{<br>        <strong>loggedIn </strong>= <strong>false</strong>;<br>      }<br>    });<br>  }  <br>  . . . .</pre><pre>  Scaffold homeScaffold = /*define as required*/;<br>  Scaffold loginScaffold = /*define as required*/;</pre><pre>  @override<br>  Widget build(BuildContext context) {<br><strong>    return new </strong>MaterialApp(<br>      title: <strong>&quot;SimpleBlogApp&quot;</strong>,<br>      home: <strong>loggedIn </strong>? homeScaffold : loginScaffold);<br>  }<br>  ....<br>  <br>  @override<br>  <strong>void </strong>initState() {<br>    <strong>super</strong>.initState();<br>    <strong>this</strong>._function();<br>    ....<br>  }<br>}</pre><h4>3. When sign in is clicked _ensureLoggedIn() function/method will be called and if it is successful then storing their User ID, User Name, User Profile Image and User emailid inside the Shared preference and making <strong>loggedIn </strong>variable true:</h4><pre>Future&lt;Null&gt; _ensureLoggedIn() <strong>async </strong>{<br>  <br>  SharedPreferences prefs;<br>  prefs = <strong>await </strong>SharedPreferences.<em>getInstance</em>();<br>  <br>  ........<br>  <br>  //sign in the user here and if it is successful then do following <br>  <br>  prefs.setString(<strong>&quot;username&quot;</strong>, user.<strong>displayName</strong>);<br>  prefs.setString(<strong>&quot;userid&quot;</strong>, user.<strong>id</strong>);<br>  prefs.setString(<strong>&quot;useremail&quot;</strong>, user.<strong>email</strong>);<br>  prefs.setString(<strong>&quot;userphotourl&quot;</strong>, user.<strong>photoUrl</strong>);<br>  <strong>this</strong>.setState(() {<br>  /*<br>     updating the value of <strong>loggedIn to true </strong>so it will <br>     automatically trigger the screen to display homeScaffold.<br>  */<br>    <strong>loggedIn </strong>= <strong>true</strong>;<br>    <strong>......</strong><br>  });<br>}</pre><h4>5. When user signs out clearing the Shared preference as:</h4><pre>Future&lt;Null&gt; logoutUser() <strong>async </strong>{<br>  <em>//logout user<br>  </em>SharedPreferences prefs;<br>  prefs = <strong>await </strong>SharedPreferences.<em>getInstance</em>();<br>  <strong>await </strong>googleSignIn.signOut();<br>  prefs.clear();<br>  prefs.commit();<br>  <strong>this</strong>.setState(() {<br>  /*<br>     updating the value of <strong>loggedIn to false </strong>so it will <br>     automatically trigger the screen to display loginScaffold.<br>  */<br>    <strong>loggedIn </strong>= <strong>false</strong>;<br>  });<br>}</pre><p>You can check the <a href="https://github.com/im-minion/Flutter-Blog-App-Firebase/blob/master/lib/HomePage.dart">HomePage.dart</a> file inside <a href="https://github.com/im-minion/Flutter-Blog-App-Firebase">Flutter blog app</a> project on the GitHub. The code is taken from that file only.</p><p>For more Flutter project by me take a look at my <a href="https://github.com/im-minion/">GitHub Profile</a> .</p><p>Happy Coding 😍<br>Thanks✌️🙂</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fdde502%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Fhackernoon%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b42e12f81eb2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/implementing-firebaseauthstatelistener-using-shared-prefrences-in-flutter-b42e12f81eb2">Implementing FirebaseAuthStateListener using Shared Prefrences in Flutter.</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>