<?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 Yoganathan Shiv on Medium]]></title>
        <description><![CDATA[Stories by Yoganathan Shiv on Medium]]></description>
        <link>https://medium.com/@yenvanio?source=rss-c28ec3653cf4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*zgccMfQgZE0v9FND0r61oQ.png</url>
            <title>Stories by Yoganathan Shiv on Medium</title>
            <link>https://medium.com/@yenvanio?source=rss-c28ec3653cf4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 07:03:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yenvanio/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[Software Design: Entropy]]></title>
            <link>https://levelup.gitconnected.com/software-design-entropy-e6c6da33f631?source=rss-c28ec3653cf4------2</link>
            <guid isPermaLink="false">https://medium.com/p/e6c6da33f631</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[software-design]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[engineering]]></category>
            <dc:creator><![CDATA[Yoganathan Shiv]]></dc:creator>
            <pubDate>Fri, 08 Apr 2022 12:52:40 GMT</pubDate>
            <atom:updated>2022-04-08T12:52:40.194Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_6sOSDAsUxFXLBicq2keMA.png" /></figure><h3>What is Entropy?</h3><p>The term was first introduced in Thermodynamics, as a quantitative measure representing the unavailability of a system’s thermal energy for conversion into mechanical work. In a broader sense, it is interpreted as the <strong>degree of disorder or randomness in the system.</strong></p><blockquote>The laws of Thermodynamics guarantee that the entropy in the universe tends towards a maximum. (Read <a href="https://www.khanacademy.org/science/ap-biology/cellular-energetics/cellular-energy/a/the-laws-of-thermodynamics">more</a>)</blockquote><h3>Software Entropy</h3><p>Although originating from physics, entropy has its place in software development as well.</p><p>Whether it be work or your own project, you will come across disorder to some extent in your software systems. When disorder increases in software, we refer to it as “Software Rot” or a more common term — “Technical Debt”.</p><p>To avoid entropy in our software systems we need to understand where it all stems from, let’s take a look at a real world example to better illustrate this problem.</p><h3>Broken Windows</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FxT5LMYncfjD9XGwjzW%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FxT5LMYncfjD9XGwjzW%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FxT5LMYncfjD9XGwjzW%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="326" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac0303b9eaf4a17902670feb40ffce19/href">https://medium.com/media/ac0303b9eaf4a17902670feb40ffce19/href</a></iframe><p>Broken Windows is a popular criminological theory stating that visible signs of crime and civil disorder create an environment that encourages further crime and disorder… and so on.</p><p>In a field study run by a psychologist from Stanford University, two cars were abandoned and parked on the street with their license plates removed for a week. For a week both cars went untouched.</p><p>At the start of the next week, the researcher smashed in a single window on one car. Before the end of the day the car was stripped for parts, more windows smashed, graffiti on the sides of the car — it was destroyed.</p><blockquote>The psychologist concluded that there was a trigger mechanism, “<strong>a broken window”</strong>.</blockquote><p>Similarly this concept can be applied to buildings, entire neighbourhoods and so on. The sense of abandonment and hopelessness that comes from ignoring a clearly broken situation often sends the message that it can’t be fixed, nobody cares or that the situation is doomed.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F3orieO2FAIRZYk8s5q%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3orieO2FAIRZYk8s5q%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F3orieO2FAIRZYk8s5q%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="331" frameborder="0" scrolling="no"><a href="https://medium.com/media/132076c80b751b46131c876462973f1c/href">https://medium.com/media/132076c80b751b46131c876462973f1c/href</a></iframe><p>These negative thoughts spread amongst everyone that passed the damaged car leading to further vandalism and damage.</p><h3>Broken Windows in Software</h3><p>In software, clean systems deteriorate faster once there is a broken window. Our goal is to avoid living with broken windows. Contributing factors often come from the psychology or culture at work, or on a specific project.</p><p>Even with the best development team, a single broken window will set precedence for the quality of code that is shipped in the future. It’s all too easy to slip into the mindset that “The codebase is crap so I will also ship subpar code”. If you keep thinking that nobody has the time to go around cleaning up all the broken glass of a project then you’re letting entropy win.</p><blockquote>Neglect accelerates software rot more than anything else</blockquote><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FYPHcJBDYqxq5a%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FYPHcJBDYqxq5a%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FYPHcJBDYqxq5a%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="337" frameborder="0" scrolling="no"><a href="https://medium.com/media/129de711ec04681fcb2901edbc96cdad/href">https://medium.com/media/129de711ec04681fcb2901edbc96cdad/href</a></iframe><p>Since this disorder can spread uncontrollably as we’ve learned from our fellow physicists, how do we fight it?</p><h3>Preventing Broken Windows</h3><p>Just as working with a crappy codebase pushes you to ship even crappier code, the same psychology applies to codebases that are pristinely written, well designed and elegantly maintained. You are inclined to take extra care not to mess it up — <strong>you don’t want to be the first one to make a mess.</strong></p><blockquote>One broken window is one too many — The Pragmatic Programmer</blockquote><p>There is no such thing as perfect software, so we can’t expect not to run into a broken window every now and then. Bad designs, wrong decisions and poor code can all lead to broken windows. These should be fixed right away, don’t leave a broken window, broken.</p><p>Even in high pressure situations where time is of the essence, board up the broken window and have a plan in place to repair it as soon as possible.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2Fl2JefhuLRZWPnxF1m%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fseason-3-the-simpsons-3x8-l2JefhuLRZWPnxF1m&amp;image=https%3A%2F%2Fmedia0.giphy.com%2Fmedia%2Fl2JefhuLRZWPnxF1m%2Fgiphy.gif%3Fcid%3D790b76114340681d26fa034fc54e3a8bb0a197d42a3cdf85%26rid%3Dgiphy.gif%26ct%3Dg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="333" frameborder="0" scrolling="no"><a href="https://medium.com/media/607565cb8bd2593324c1113b27754fec/href">https://medium.com/media/607565cb8bd2593324c1113b27754fec/href</a></iframe><p>I hope you found some worthwhile information while reading through this post. If you want to apply some of these lessons in your projects, I’ve listed some suggestions below. <em>Heres to building better systems! 🍻</em></p><h4>Suggestions</h4><p>Help strengthen your team by surveying the project “neighbourhood”. Choose a few broken windows, discuss with your team how you will fix them and begin repairing the windows.</p><blockquote>Make this a recurring event — mechanisms work, good intentions do not.</blockquote><p>Ask yourself and your team the following questions when a broken window is identified…</p><ul><li>Can you tell when it was first broken?</li><li>What’s your teams reaction to a broken window?</li><li>What was this broken window a result of?</li><li>What can you do about this broken window?</li></ul><p><em>Thank you for reading! If you liked this article and want to learn more about software design </em><a href="https://yenvanio.medium.com/subscribe"><em>subscribe to my email list</em></a><em> and give me a follow! </em><a href="https://yenvanio.medium.com/membership"><em>Become a member</em></a><em> to support the community of writers on Medium.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e6c6da33f631" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/software-design-entropy-e6c6da33f631">Software Design: Entropy</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Software Design — Orthogonality]]></title>
            <link>https://levelup.gitconnected.com/software-design-orthogonality-7cd2d50267b6?source=rss-c28ec3653cf4------2</link>
            <guid isPermaLink="false">https://medium.com/p/7cd2d50267b6</guid>
            <category><![CDATA[software-design]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[engineering]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Yoganathan Shiv]]></dc:creator>
            <pubDate>Fri, 10 Dec 2021 14:25:10 GMT</pubDate>
            <atom:updated>2022-01-12T21:46:06.465Z</atom:updated>
            <content:encoded><![CDATA[<h3>Software Design — Orthogonality</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*xyeky8KiZiOvWAMArkTt2Q.png" /><figcaption>Credit to <a href="https://www.instagram.com/maldonaut/"><strong>maldonaut</strong></a></figcaption></figure><blockquote>Software Design can seem tricky or convoluted, and it can get away from you. Luckily with many years of evolving software development, there are well established principles and guidelines for designing clean, robust software.</blockquote><blockquote>One of these principles is Orthogonality, so let’s take a look at what that means and why it’s important in Software Development and Design.</blockquote><h3>What is Orthogonality?</h3><p>The origins of orthogonality come from math! Two lines are orthogonal if they meet at a right angle. It can also be explained with vectors — <em>two vectors are orthogonal if they are independent</em>. <em>(</em><a href="https://en.wikipedia.org/wiki/Orthogonality"><em>Read More</em></a><em>)</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*ipugzzv8p_KG2Jxq6I9FZA.png" /></figure><h3>But… we’re writing code!</h3><p>In software development, we use Orthogonality to define the separation or independence between software components. In its simplest terms it means <strong>Changing A does not change B</strong>.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FQmkMGTMpGELDi%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Freaction-lilo-and-stitch-touch-QmkMGTMpGELDi&amp;image=https%3A%2F%2Fmedia3.giphy.com%2Fmedia%2FQmkMGTMpGELDi%2F200.gif%3Fcid%3D790b76118a8422bd559c1fa6764aa27bb72106f319003d30%26rid%3D200.gif%26ct%3Dg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="233" frameborder="0" scrolling="no"><a href="https://medium.com/media/b3d7415ea307f009f377b8d542b3bf29/href">https://medium.com/media/b3d7415ea307f009f377b8d542b3bf29/href</a></iframe><p>Although it is naive to believe that your system can be 100% orthogonal. The goal here is to follow this principle when designing your systems to achieve the best possible results.</p><p>A simple real world example is a radio, where changing the station does not change the volume and vice versa. A non-orthogonal system would be a helicopter where changing speed, can affect the direction.</p><blockquote>Why is that important?</blockquote><p>Writing self contained, independent components with a single defined purpose will enable you to build a software system that is easier to develop, test, scale and minimize the risk of errors.</p><blockquote>How does one principle enable all of that?</blockquote><p>Imagine having a database that needs to change every time you change a field on the frontend interface. That sounds horrible right? This is why it’s important to decouple software components. This is a simple and quite obvious example but we will dive into more examples later in the article that may be overlooked in software development</p><h3>Exploring Orthogonality in Code</h3><p>To understand what orthogonality might look like in code, we’re going to be looking at a <strong>NonOrthogonalUser</strong> class and an <strong>OrthogonalUser</strong> class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a9d1b3aa57e52f9888cd34dcfebe607c/href">https://medium.com/media/a9d1b3aa57e52f9888cd34dcfebe607c/href</a></iframe><h4>What makes this Non-Orthogonal?</h4><ul><li>As a normal object class in Java it has a few properties defined and a constructor to initialize it. Now there’s clearly a property that sticks out — textFileStrings.</li></ul><blockquote>The User class is handling a task it shouldn’t be responsible for, causing it to be tightly coupled to the loadFile() function as well as the values it returns.</blockquote><ul><li>Changes made to the loadFile() function or the format of the data being returned will ripple through the User class resulting in modifications in this class as well.</li><li>Placing this logic within the User class prevents it from being reused separately. In addition the knowledge and functionality cannot be tested separately either.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/88622926f39e441fa48d3f5ff3bd3944/href">https://medium.com/media/88622926f39e441fa48d3f5ff3bd3944/href</a></iframe><h4>What makes this Orthogonal?</h4><ul><li>Now this User class conforms to what you would normally expect.</li></ul><blockquote>The CustomerTextObjectReader class contains the functionality for parsing the text file and is dedicated to performing that single function.</blockquote><ul><li>Say, we wanted to modify how we load the data — use a database or fetch data from an API, the reader class can be modified to serve that purpose without affecting the User class.</li><li>If the data object changes, only the CustomTextObject and the CustomerTextObjectReader classes would require changes.</li><li>Finally to really nail this point home, testability is improved as well — it can be tested separately because the functionality is nicely encapsulated. You can test the functionality with module or unit level tests. These are much easier to implement than integration tests for example.</li></ul><h3>Benefits of Orthogonality</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FwRgtUUCRA8uQ42TnxS%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fjohnbcrist-comedy-john-crist-johncrist-wRgtUUCRA8uQ42TnxS&amp;image=https%3A%2F%2Fmedia4.giphy.com%2Fmedia%2FwRgtUUCRA8uQ42TnxS%2Fgiphy.gif%3Fcid%3D790b76118c765271a62bc5312bd07dad680e01b184e84378%26rid%3Dgiphy.gif%26ct%3Dg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="240" frameborder="0" scrolling="no"><a href="https://medium.com/media/84f09efb4a207e713ac7c55bd0bcccfd/href">https://medium.com/media/84f09efb4a207e713ac7c55bd0bcccfd/href</a></iframe><h4>Productivity</h4><p>Writing tests and even development time is reduced with orthogonal systems because the components are self contained. I think we can all appreciate writing tests for an isolated component rather than worrying about all the different connected parts that need to be incorporated for tests to run.</p><h4>Reusability</h4><p>When we set out to design orthogonal systems, each component should be self contained, independent and have a single well-defined purpose. This promotes reusability because when you incorporate them into multiple places you shouldn’t have to worry about unintended side effects.</p><p>Components can be reconfigured and re-engineered for different purposes, even if not originally envisioned by the author of the component.</p><h4>Testing &amp; Minimizing Risk</h4><p>Orthogonal systems are much simpler to test, they are usually performed at the module or unit level. As mentioned above this is much more preferable to integration tests where you need to worry about setting up multiple other components to test the simple functionality of a Reader class.</p><p>When one part of the system is failing, it will be contained. They can be seen as “diseased” modules and with an orthogonal approach should be isolated so it doesn’t spread.</p><p>Additionally it makes debugging and fixing bugs simpler due to the isolation of components, bugs will be localized and easier to locate than in a highly coupled system.</p><p>If truly orthogonal, then the interfaces you surface to communicate with external vendors should be reusable with multiple vendors to avoid vendor lock.</p><h3>Designing Orthogonal Systems</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FxUA7b0Klw8Wfor7FWo%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Floop-play-colors-xUA7b0Klw8Wfor7FWo&amp;image=https%3A%2F%2Fmedia3.giphy.com%2Fmedia%2FxUA7b0Klw8Wfor7FWo%2Fgiphy.gif%3Fcid%3D790b761173bf2ad69249bacd75882d99e03a91aad8037ca6%26rid%3Dgiphy.gif%26ct%3Dg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="326" frameborder="0" scrolling="no"><a href="https://medium.com/media/81f343dcd682da35f43e9ceff0041a2f/href">https://medium.com/media/81f343dcd682da35f43e9ceff0041a2f/href</a></iframe><p>By this point you probably get the gist of what it means to design an Orthogonal system… and we’re almost done, I promise! Here are some final thoughts to keep in mind when designing your system.</p><ul><li>An orthogonal system contains a set of cooperating modules. Each module serving a single purpose and implements that functionality independent of other modules.</li><li>Abstraction is a key component of Orthogonal systems. Layers of abstraction only use the abstraction provided by the layer below, this allows more flexibility to change underlying implementation without affecting other code.</li><li>3rd party libraries can heavily simplify your workload, but be wary that it doesn’t impose changes on your code that shouldn’t be there. As mentioned before keeping these isolated from your code helps you avoid vendor lock. <strong>Don’t rely on the property of things you can’t control.</strong></li></ul><blockquote>If you dramatically change the requirements behind a particular function, how many modules should be affected? The answer should be 1.</blockquote><blockquote>— The Pragmatic Programmer</blockquote><p>I hope you found some worthwhile information while reading through this post. If you want to dig further into this principle, I’ve listed some related topics below. <em>Heres to building better systems! 🍻</em></p><h4>DRY Principle</h4><ul><li>Don’t Repeat Yourself</li></ul><h4>SHY Principle</h4><ul><li>Don’t Reveal Yourself to Others</li><li>Modules that don’t reveal anything unnecessary to others, and don’t rely on others for implementation</li></ul><h4>Law of Demeter</h4><ul><li>The Law of Demeter principle states that a module should not have the knowledge on the inner details of the objects it manipulates. In other words, a software component or an object should not have the knowledge of the internal working of other objects or components.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7cd2d50267b6" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/software-design-orthogonality-7cd2d50267b6">Software Design — Orthogonality</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Chat Application — Flutter & Firebase]]></title>
            <link>https://medium.com/flutter-community/a-chat-application-flutter-firebase-1d2e87ace78f?source=rss-c28ec3653cf4------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d2e87ace78f</guid>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[messaging]]></category>
            <dc:creator><![CDATA[Yoganathan Shiv]]></dc:creator>
            <pubDate>Tue, 29 Dec 2020 21:20:24 GMT</pubDate>
            <atom:updated>2021-01-31T00:20:11.532Z</atom:updated>
            <content:encoded><![CDATA[<h3>A Chat Application — Flutter &amp; Firebase</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F26FPJGjhefSJuaRhu%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fchat-conversation-gifathon-26FPJGjhefSJuaRhu&amp;image=https%3A%2F%2Fmedia1.giphy.com%2Fmedia%2F26FPJGjhefSJuaRhu%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="326" frameborder="0" scrolling="no"><a href="https://medium.com/media/19f3f2d0f6e1c4d5aa92dad8313f8f7e/href">https://medium.com/media/19f3f2d0f6e1c4d5aa92dad8313f8f7e/href</a></iframe><p>In my previous articles I have gone over the basics of Flutter &amp; Firebase, including authenticating users with Firebase. If you haven’t checked these out already, I would <strong>highly recommend</strong> going over them before continuing with this article — <a href="https://medium.com/xenon-labs/simple-to-do-list-app-in-flutter-26abc50db741">Getting Started With Flutter</a>, <a href="https://medium.com/xenon-labs/flutter-firestore-crud-operations-8a7004b97d71">Configuring Flutter with Firebase</a>, <a href="http://g/">Firebase and User Authentication</a>.</p><h3>Database Schema</h3><p>This article assumes you have an existing Flutter application that is connected to a Cloud Firestore database, with an existing method of user authentication. Take a look at my <a href="http://g/">Firebase and User Authentication</a> article if you need help getting started with that. Before we dive into coding it is important to discuss the structure of our database and how we can efficiently store and retrieve our data.</p><h4><strong>User Collection</strong></h4><p>We need a collection to store User data. The User collection is kept simple, as described below. We will be using <strong>Google Sign In</strong> to authenticate users, and to write required data such as name and email into our User collection</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KwuGGAyZjrwM2nwf7oLVKQ.png" /><figcaption>Firebase Console — Displaying User Collection</figcaption></figure><p><strong>Messaging Collection<br></strong>In addition to a User collection, we need another collection to store the messages between our users. Each document in the Messages collection will represent a conversation. Now we don’t want to store the list of messages in an array or map because Firebase has size limitations for individual documents but none for the number of documents in a collection. Refer to the <a href="https://firebase.google.com/docs/firestore/quotas">Usage and Limits Documentation</a> for more information.</p><blockquote>The ideal way to store data in Firebase is to have a collection of small documents rather than a single large document. —<a href="https://firebase.google.com/docs/firestore/best-practices"> Firestore Best Practices</a></blockquote><p>So, we create a subcollection! By doing so we can have any number of <em>messageDocuments </em>stored in the collection. See the screenshot below to get a better understanding.</p><p><em>(Don’t worry about the document and collection ID’s, we’ll get to that shortly.)</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GgJJuBpFvTknNrJX7AEz-A.png" /><figcaption>Firebase Console — Displaying Messages Collection Data</figcaption></figure><p>Now, in our application we need to be able to retrieve this data — a conversation between two users. When a user selects a contact to chat with, we have both their userID&#39;s and using this we can come up with an efficient way to query our data.</p><p>A simple, yet effective technique is to store each conversationID as a combination of both users userID . We also need to ensure we can successfully generate the same combination every time.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b43652d0118772eae78d2680fac610d9/href">https://medium.com/media/b43652d0118772eae78d2680fac610d9/href</a></iframe><p>We can now get all the messages from a conversation using the code below.</p><pre>Firestore.<strong><em>instance</em></strong><em><br>         </em>.collection(&#39;messages&#39;)<br>         .document(<strong>convoID</strong>)<br>         .collection(<strong>convoID</strong>).snapshots();</pre><h4>Taking our collection to the next level</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/567/1*7XQlSNSG5b5Ps9vuF6uJ6w.png" /><figcaption>Whatsapp Conversation List</figcaption></figure><p>Typically a chat application will display a list of all your conversations on the home screen.</p><p>On the left, you’ll see the Chats screen from Whatsapp. This is what we are aiming to create.</p><p>These conversations are a preview of sorts, and<strong> </strong>will contain the last message sent between users along with a timestamp.</p><p>Our schema does not support an efficient way to query this data, we would have to go into our subcollection and retrieve the last message to do so. In addition to that we can’t query all the conversations belonging to a user without checking the content of the individual messages.</p><p>You may have noticed earlier but our conversation document is empty, aside from containing a subcollection of messages. We need to add our required data here to solve our querying problem. We will be adding a lastMessage<br>object and the userID&#39;s of both users.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9mja_KRFWwWPvqWIt1kFAA.png" /></figure><p>The updated structure lets us query all conversations for a user and retrieve the last message, with the single query you see below.</p><pre>Firestore.<strong><em>instance</em></strong><em><br>         </em>.collection(&#39;messages&#39;)<br>         .where(&#39;users&#39;, arrayContains: userId)<br>         .snapshots(); </pre><p>Now that we have our database schema in place, we can jump right into coding!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F1ziiQ8TVfLgeGWUOFx%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fdesusandmero-excited-hype-1ziiQ8TVfLgeGWUOFx&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F1ziiQ8TVfLgeGWUOFx%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/78d94bb08cbb74c92aeb787c6b02f3d8/href">https://medium.com/media/78d94bb08cbb74c92aeb787c6b02f3d8/href</a></iframe><h3>Creating a User Directory</h3><p>The first thing we need to do, before a user ever sends a message is to give them the ability to search for recipients. To start, the code below defines the Home screen of the application <strong>after</strong> a user has logged in.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b3b8e689a6e552fb2c82c98b08a11043/href">https://medium.com/media/b3b8e689a6e552fb2c82c98b08a11043/href</a></iframe><h4>Breaking down the code</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/243/1*FD7I2L5edIIa3m6jfjAuwA.png" /></figure><ul><li>The App Bar allows users to logout (left) or create a new conversation (right). The user’s name is also displayed here.</li><li>The body is empty for now, we will be updating this after we generate some conversations.</li><li>When the user wants to create a new conversation, the app will navigate to the user directory screen.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/71b235ab2a20c0a9c9de35888c4ace7e/href">https://medium.com/media/71b235ab2a20c0a9c9de35888c4ace7e/href</a></iframe><blockquote>We are using a StreamProvider , this allows new users that are added to the database to automatically show up on the user directory screen, without the need for a user to manually refresh the page. This can be done without a StreamProvider if you don’t want to actively listen for changes.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd1f6adaf5d96c8cbe07698ee2e8856d/href">https://medium.com/media/fd1f6adaf5d96c8cbe07698ee2e8856d/href</a></iframe><blockquote>The streamUsers function is very straightforward, it grabs all the documents from the user collection and maps them to a custom object. This is then streamed to a widget.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/84d0d9b4b44839a84896ef73fba3e906/href">https://medium.com/media/84d0d9b4b44839a84896ef73fba3e906/href</a></iframe><blockquote>The user model needs to map the data from Firestore into a custom object.</blockquote><h3>Creating a Conversation</h3><p>We now have a list of users, with which we can create a User Directory Screen. The code below displays a ListView of user items.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba73e40fb4c80c552424c94d35c495da/href">https://medium.com/media/ba73e40fb4c80c552424c94d35c495da/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/243/1*vTA65Cl7OZtSUiIeBOCpdA.png" /><figcaption>User Directory</figcaption></figure><h4>Breaking down the code</h4><ul><li>Scaffold checks if the user stream is null before generating any widgets</li><li>Listview has a list of all the users (excluding the current one)</li><li>I externalized the UserRow widget which essentially displays a contact’s name and onTap proceeds to create a conversation with said contact.</li><li>View the userRow.dart code below</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c6c9bbb975121712529864d77057355/href">https://medium.com/media/2c6c9bbb975121712529864d77057355/href</a></iframe><h3>Quick Recap</h3><h4>So far we have…</h4><ul><li>Home Page — Logout / Create a new conversation</li><li>User Directory Screen — Select a user to chat with</li></ul><h4>What we’re going to be implementing next…</h4><ul><li>Building our ChatScreen UI</li><li>Streaming all the messages in a conversation</li><li>Sending messages between users</li><li>Dealing with Read &amp; Unread messages</li><li>Fetching a list of all conversations for a user</li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F3o751RBT4WdcEi4o4E%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fwwe-wrestling-3o751RBT4WdcEi4o4E&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3o751RBT4WdcEi4o4E%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="243" frameborder="0" scrolling="no"><a href="https://medium.com/media/e926fdad15e7e1d0cd206405a719a97d/href">https://medium.com/media/e926fdad15e7e1d0cd206405a719a97d/href</a></iframe><blockquote>Don’t worry! It may seem like a lot of code coming up, but I’ll be sure to break it down into consumable portions.</blockquote><h3>Chat Screen UI</h3><p>I’ve constructed a very barebones UI as you can see in the image below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/318/1*29iReYt9OE6QFi4La5ZspQ.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d07dae7f472fea9e0194a00703693d7a/href">https://medium.com/media/d07dae7f472fea9e0194a00703693d7a/href</a></iframe><ul><li>NewConversationScreen contains the data we passed in from the userRow widget and it builds a simple app-bar containing the contacts name</li><li>ChatScreen is the crux of this code. It will build majority of our UI including the list of messages and input box.</li><li>I’ve left placeholder functions for now, and will go through each part separately below.</li></ul><h4>Input Widget</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/227e04b7d211729e76c348e274495c2f/href">https://medium.com/media/227e04b7d211729e76c348e274495c2f/href</a></iframe><ul><li>Input Widget consists of a simple TextField and an IconButton to send the message.</li></ul><h4>Message List Widget</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c293c3c8212d8862d811cdb6a9e0f84d/href">https://medium.com/media/c293c3c8212d8862d811cdb6a9e0f84d/href</a></iframe><ul><li>Using a StreamBuilder we can grab a collection of documents to populate our builder function. In this scenario I am retrieving the last 20 messages sent between two users and using the buildItem function below to generate the chat bubbles.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4195f266b25a316bfb1bc6a04a691c67/href">https://medium.com/media/4195f266b25a316bfb1bc6a04a691c67/href</a></iframe><ul><li>This function updates the message read status using the following function. We want to ensure we do this here, so when we update the lastMessage field we will have the correct data.</li></ul><pre><strong>final</strong> DocumentReference documentReference = Firestore.<strong><em>instance</em></strong><em><br>    </em>.collection(&#39;messages&#39;)<br>    .document(convoID)<br>    .collection(convoID)<br>    .document(doc.<strong>documentID</strong>);<br><br>documentReference.setData(&lt;String, dynamic&gt;{&#39;read&#39;: true}, merge: true);</pre><ul><li>It also checks if the message is to/from the current user to build the messages on the appropriate side.</li><li>The last piece of code for the chat screen is the onSendMessage function</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f4985e3f8383dc0b4ebdf50a4a9cf7c/href">https://medium.com/media/1f4985e3f8383dc0b4ebdf50a4a9cf7c/href</a></iframe><ul><li>We are checking if there is a message ready to send, and upon sending we clear the input field</li><li>When sending the message we also update the lastMessage field in the conversation document and set read to false. (We’ve seen earlier, it is set to read when the recipient streams the messages)</li><li>Now we have a fully functioning chat screen, that should look similar to the picture from the start of this section.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/318/1*29iReYt9OE6QFi4La5ZspQ.png" /></figure><h3>Viewing Conversations</h3><p>Now we can send messages between users, but we also want to be able to view a list of open conversations on our home screen. To do that we need to stream conversations from our Firestore messages collection.</p><p>Earlier in our Firestore setup, we added a users field to every conversation document. This field is an array containing the IDs of both users, now that we have this information available at the document level we can query it very easily.</p><pre><strong>static</strong> Stream&lt;List&lt;Convo&gt;&gt; <strong><em>streamConversations</em></strong>(String uid) {<br>  return <strong><em>_db</em></strong><em><br>      </em>.collection(&#39;messages&#39;)<br>      .orderBy(&#39;lastMessage.timestamp&#39;, descending: true)<br>      .where(&#39;users&#39;, arrayContains: uid)<br>      .snapshots()<br>      .map((QuerySnapshot list) =&gt; list.<strong>documents</strong><br>          .map((DocumentSnapshot doc) =&gt; Convo.fromFireStore(doc))<br>          .toList());<br>}</pre><p>In addition to retrieving the conversations, we need user data as well to populate fields like displayName and profileImage</p><pre>static Stream&lt;List&lt;User&gt;&gt; <strong><em>getUsersByList</em></strong>(List&lt;String&gt; userIds) {<br>  final List&lt;Stream&lt;User&gt;&gt; streams = <strong>List</strong>();<br>  for (String id in userIds) {<br>    streams.add(<strong><em>_db</em></strong><em><br>        </em>.collection(&#39;users&#39;)<br>        .document(id)<br>        .snapshots()<br>        .map((DocumentSnapshot snap) =&gt; User.fromMap(snap.<strong>data</strong>)));<br>  }<br>  return <strong>StreamZip</strong>&lt;User&gt;(streams).asBroadcastStream();<br>}</pre><p>This will retrieve the user data for every user you are currently having a conversation with.</p><blockquote>Note: This is not the best approach in terms of scalability and performance. I am using this approach for quick development and would not recommend this approach in production.</blockquote><h3>Putting it all Together</h3><p>I’ve constructed a simple listview to display all active user conversations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/315/1*-lWi4Ee3_Pezt0huTeFsRA.png" /><figcaption>Conversation List Screen</figcaption></figure><p>We are going to start off by modifying our HomeScreen</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e483737c629a47d52a27fb15f86e9d2c/href">https://medium.com/media/e483737c629a47d52a27fb15f86e9d2c/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/439e362638f3fed34df17d7607477455/href">https://medium.com/media/439e362638f3fed34df17d7607477455/href</a></iframe><ul><li>I’ve modified HomeScreen to use a Provider that streams conversations and users, that way all the data we need will be readily available in our HomeBuilder widget.</li><li>ConversationProvider streams the conversations to it’s childrenConversationDetailsProvider and HomeBuilder</li><li>ConversationDetailsProvider streams the users to its child HomeBuilder</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/25b424b64d2c0fcab4e8809f720391d3/href">https://medium.com/media/25b424b64d2c0fcab4e8809f720391d3/href</a></iframe><ul><li>HomeBuilder now has all the necessary information ready by using our Provider (Line 14 &amp; 15)</li><li>It then uses the data to build ConvoListItem widgets</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8fab6fa4c9f76faf23d34ecb8b338e31/href">https://medium.com/media/8fab6fa4c9f76faf23d34ecb8b338e31/href</a></iframe><ul><li>The convoWidget formats the data and displays it to the user with lastMessage timestamp recipientName</li><li>In the build function you may have noticed it also determines if the lastMessage was read and adds a dot indicator for unread messages.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/315/1*-lWi4Ee3_Pezt0huTeFsRA.png" /><figcaption>Conversation List Screen</figcaption></figure><h3>Summary &amp; Next Steps</h3><p>In this article we’ve successfully put together a chat application where you can</p><ul><li>Search for users</li><li>Message a user</li><li>View a list of open conversations</li><li>View Read / Unread conversations</li></ul><p>All of the code from this article can be viewed at the Github link below, in a sample application created by Xenon Labs.</p><p><a href="https://github.com/Xenon-Labs/Flutter-Development/tree/master/chat_app">Xenon-Labs/Flutter-Development</a></p><p>Follow Flutter Community on Twitter: <a href="https://www.twitter.com/FlutterComm">https://www.twitter.com/FlutterComm</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1d2e87ace78f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/flutter-community/a-chat-application-flutter-firebase-1d2e87ace78f">A Chat Application — Flutter &amp; Firebase</a> was originally published in <a href="https://medium.com/flutter-community">Flutter Community</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[User Authentication — Flutter & Firebase]]></title>
            <link>https://medium.com/xenon-labs/user-authentication-flutter-firebase-5f733f3e3dd?source=rss-c28ec3653cf4------2</link>
            <guid isPermaLink="false">https://medium.com/p/5f733f3e3dd</guid>
            <category><![CDATA[mobile]]></category>
            <category><![CDATA[authentication]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[firestore]]></category>
            <dc:creator><![CDATA[Yoganathan Shiv]]></dc:creator>
            <pubDate>Fri, 10 Jul 2020 13:14:53 GMT</pubDate>
            <atom:updated>2020-07-10T13:14:53.541Z</atom:updated>
            <content:encoded><![CDATA[<h3>User Authentication — Flutter &amp; Firebase</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UuTVOK5AODCGh_YghzKfEA.png" /></figure><p>This article will be covering <strong>User Authentication</strong> using Firebase.<strong> </strong>In my previous articles I have gone over the basics of Flutter &amp; Firebase and how you can build a simple application connected to a cloud database. If you haven’t checked these out already, I would recommend going over them before continuing with this article — <a href="https://medium.com/xenon-labs/simple-to-do-list-app-in-flutter-26abc50db741">Getting Started With Flutter</a>, <a href="https://medium.com/xenon-labs/flutter-firestore-crud-operations-8a7004b97d71">Configuring Flutter with Firebase</a>.</p><h3>Setup</h3><p>Firebase offers many options for third-party user authentication, the simplest being Google, which uses a gmail account to authenticate the user. Refer to the image below and the following instructions on how to enable Google Sign In on your Firebase console.</p><h4>Firebase Setup</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vdHGmP7rOBsKIWqTR5kBYg.png" /><figcaption>Firebase Authentication Console</figcaption></figure><p>To enable Google Authentication</p><ul><li>Click the <strong>Authentication</strong> tab from the navigation menu</li><li>Click the <strong>Sign in Method</strong> tab</li><li>Find <strong>Google</strong> in the list of sign-in options</li><li>Click <strong>Enable</strong></li></ul><h4>Flutter Setup</h4><p>To get started with authentication, there are a few packages you need to install. Add the following dependencies to your pubspec.yaml file.</p><pre><strong>provider:</strong> ^3.0.0+1<br><strong>google_sign_in:</strong> ^4.1.1<br><strong>cloud_firestore:</strong> ^0.13.0+1<br><strong>firebase_core:</strong> ^0.4.0<br><strong>firebase_auth</strong>: ^0.15.3+1</pre><ul><li><strong>Provider:</strong> Allows inheritance between parent and child widgets</li><li><strong>Google Sign In:</strong> Flutter plugin for Google Sign In</li><li><strong>Cloud Firestore:</strong> Communicates with our remote Firestore database</li><li><strong>Firebase Core: </strong>Allows connections with Firebase applications</li><li><strong>Firebase Auth: </strong>Manages user authentication</li></ul><blockquote>Before going any further, I thought it might be worthwhile to share my project structure. It’s always good to keep your files organized. At this point, it looks a little something like this</blockquote><pre>/chat_app<br>  ...<br>  - lib<br>    - /providers<br>    - /screens<br>    - /services<br>    - main.dart<br>  ...</pre><ul><li><strong>Providers</strong> — Here, we will be creating a Provider widget. This will allow us to track the user’s authentication state. We need to know when the user is logged in vs logged out to determine what screen to display.</li><li><strong>Screens</strong> — This is where I will be adding the LoginScreen &amp; HomeScreen . Initially we will be displaying the LoginScreen and upon successful authentication we will display the HomeScreen .</li><li><strong>Services</strong> — Will contain an Authentication file which will handle login and logout, Database file to communicate with our Cloud Firestore Database.</li></ul><h3>Providers</h3><p>A Provider can be used to provide a value to anywhere in the widget tree. However, it won’t update the widget tree when that value changes.</p><p>For example, if you pass in a data-model-object User to a Provider it can be retrieved using a Consumer , however it will only retain the initial value. When the User model gets updated, the update is not received by the Consumer .</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FeNMa1YRPDH3u2EITh6%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2FBounce-TV-watching-observing-taking-notes-eNMa1YRPDH3u2EITh6&amp;image=https%3A%2F%2Fmedia1.giphy.com%2Fmedia%2FeNMa1YRPDH3u2EITh6%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="435" frameborder="0" scrolling="no"><a href="https://medium.com/media/2eca6fd431305b336782401ba831ba1e/href">https://medium.com/media/2eca6fd431305b336782401ba831ba1e/href</a></iframe><p>Luckily, there are different types of Providers we can use, that enable us to listen to these changes.</p><p>In our case, we want to listen to updates of the User as the authenticated state changes.</p><p>The easiest way to track the state of a user is to use a StreamProvider .</p><p>With this, flutter will continuously listen to changes and update all the Consumers accordingly.</p><p>I have removed the boilerplate code from main.dart and repurposed it with a custom Provider widget.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4bbb9e6abd9ede3bc4714bd28344e184/href">https://medium.com/media/4bbb9e6abd9ede3bc4714bd28344e184/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c00db5607bc541a2793039e0c8dd00aa/href">https://medium.com/media/c00db5607bc541a2793039e0c8dd00aa/href</a></iframe><h4>Breaking down the code</h4><ul><li>StreamProvider listens to changes of aFirebaseUser object.</li><li>It passes the valueof a FirebaseUser object whenever the auth state changes.</li><li>The updated object is being passed to the child — a Material Application, which will allow all the widgets in the application to recieve the data.</li></ul><h3>Consuming Data</h3><p>Now that we have setup our StreamProvider with thevalue and child parameters, we can consume the data. In our child widget Home we can grab data from the provider using the line of code below.</p><p>Provider.of&lt;FirebaseUser&gt;(context)</p><p>This allows the child widget to consume Provider data within it’s build function. I’ve added the code below, which checks if a user is logged in and returns either a LoginScreen to prompt the user to login or the HomeScreen for logged in users.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1e4136d59471c2e57e28384acdd293ba/href">https://medium.com/media/1e4136d59471c2e57e28384acdd293ba/href</a></iframe><h3>Authenticated State</h3><p>Now that we have a foundation setup we can build our LoginScreen &amp; our HomeScreen . I have created a very simple LoginScreen below comprised of a single button that will allow the user to sign in using their Google Credentials.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/843b37e0cb7cf2b1cdeeaba83a349cea/href">https://medium.com/media/843b37e0cb7cf2b1cdeeaba83a349cea/href</a></iframe><h4>Handling Auth Functions</h4><p>The authentication is handled in a separate file /lib/services/authentication.dart . The code below, essentially uses packages we installed earlier to handle the authentication. Firebase also lets us know if this is a new user or a returning user. With this information we can decide if we want to add the user data to our database.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bd1375b48e2d368afbc0e81e0a63bea2/href">https://medium.com/media/bd1375b48e2d368afbc0e81e0a63bea2/href</a></iframe><h4>Connecting with Firestore</h4><p>The code for the database is shown below — a simple write transaction. The purpose for adding this data to our Cloud Firestore is so that you can modify it in the future. Additional user data should be saved to a document in your Firestore database.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1cfa6ba0ea12d25d4f52a1900e1a8e68/href">https://medium.com/media/1cfa6ba0ea12d25d4f52a1900e1a8e68/href</a></iframe><p>Finally a simple UI for our HomeScreen . This will be displayed once a user logs in.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d4804ac3df8757e8c7dfadd431e5c2e3/href">https://medium.com/media/d4804ac3df8757e8c7dfadd431e5c2e3/href</a></iframe><blockquote>Note that I can use the Provider in this widget as well, because the StreamProvider was declared at the top level of the application.</blockquote><blockquote>Now all the children widget are able to retrieve the FirebaseUser object using this one simple line of code,</blockquote><blockquote>Provider.of&lt;FirebaseUser&gt;(context) .</blockquote><h3>Summary &amp; Next Steps</h3><p>Checkout the demo below, to view the app change views along with the user authenticated state.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bgr7W5Eez_uzmgSQbZPIbA.gif" /></figure><p>In this tutorial, I have demonstrated the ease of authenticating your users using Firebase, Google Sign In &amp; Cloud Firestore. You can view the source code <a href="https://github.com/Xenon-Labs/Flutter-Development/tree/master/chat_app">here</a>. If you are interested in furthering the application, I have listed potential areas for continued development below.</p><ul><li>Add Facebook and Apple Sign in</li><li>Retrieve user data from Firestore upon successful login</li><li>Learn about all the Providers <a href="https://medium.com/flutter-community/making-sense-all-of-those-flutter-providers-e842e18f45dd">here</a>, and see if there is a better fit for you</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5f733f3e3dd" width="1" height="1" alt=""><hr><p><a href="https://medium.com/xenon-labs/user-authentication-flutter-firebase-5f733f3e3dd">User Authentication — Flutter &amp; Firebase</a> was originally published in <a href="https://medium.com/xenon-labs">Xenon Labs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Flutter & Firestore CRUD operations]]></title>
            <link>https://medium.com/flutter-community/flutter-firestore-crud-operations-8a7004b97d71?source=rss-c28ec3653cf4------2</link>
            <guid isPermaLink="false">https://medium.com/p/8a7004b97d71</guid>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[mobile]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[flutter]]></category>
            <dc:creator><![CDATA[Yoganathan Shiv]]></dc:creator>
            <pubDate>Mon, 13 Apr 2020 17:41:11 GMT</pubDate>
            <atom:updated>2021-11-25T05:24:12.930Z</atom:updated>
            <content:encoded><![CDATA[<h3>CRUD operations — Flutter &amp; Firebase</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*mf9ymC80GDormlUvT2uPDA.png" /></figure><p>In this article, I will be configuring an existing Flutter application with Firebase. This tutorial covers basic setup and <strong>CRUD</strong> operations. If you have not checked out my previous post on building a <a href="https://medium.com/xenon-labs/simple-to-do-list-app-in-flutter-26abc50db741">Todo List App</a> with Flutter, it is worthwhile to familiarize yourself with it before continuing. In this tutorial we will be integrating the <strong>to-do</strong> app with Firebase Cloud Firestore.</p><h3>Getting Started</h3><p>To kickoff this tutorial, we will head on over to <a href="http://firebase.google.com/">Firebase</a> and create a project. Once you have gone through the project creation flow you will be redirected to a page similar to the one you see below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k7lf1EzI-za8U6gyx-NfmQ.png" /><figcaption>Firebase new project page</figcaption></figure><p>After creating your project, you will need to grab the configuration details from the project settings. You can find it located in the top left corner (the gear icon beside ‘Project Overview’).</p><p>Firebase has an easy setup process to follow for both Android and iOS. I will be starting with the Android setup.</p><h3>Android Setup</h3><p>Once you select the Android icon from the project settings page, you will see the following.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T5hX0NSr24T2rPqo_rGNCg.png" /><figcaption>Registering Firebase with Android</figcaption></figure><ol><li>The package name can be found in the AndroidManifest.xml file located at /android/app/main/AndroidManifest.xml</li><li>To get the SHA-1 key you can run the following command</li></ol><pre>// Mac<br>keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android</pre><pre>// Windows<br>keytool -list -v -keystore &quot;\.android\debug.keystore&quot; -alias androiddebugkey -storepass android -keypass android</pre><p>Once you’ve registered your app, you can download the google-services.json file and add it to your project in the /android/app folder. You can now run your app and verify installation on the firebase page.</p><p>You will also need to add the google-services dependency into your android app. Add the following line to /android/build.gradle under dependencies</p><pre>classpath &#39;com.google.gms:google-services:4.3.3&#39;</pre><blockquote>At the time of writing this article I was using version 4.3.3, ensure your versions are up to date, to avoid running into any errors further down the road.</blockquote><h3>iOS Setup</h3><p>To start this setup you can find the iOS icon at the bottom of the page (if you have already linked an app before, you click the Add App option).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y2GpU5IUBROcjMe5kaOETg.png" /><figcaption>Registering Firebase with iOS</figcaption></figure><p>The iOS bundle ID can be found by opening Runner.xcodeproj from /ios/Runner.xcodeproj</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZuRhptWYyx2VMn_L3l5pUA.png" /><figcaption>Getting the iOS Bundle Identifier from Xcode</figcaption></figure><p>Download the GoogleService-info.plist file into your /ios/Runner folder. Now with Xcode still open you need to drag that file into the Runner folder in the side navigation. When prompted, add to all targets.</p><p>The next step is to add Firebase to your pod file. If you do not have one yet, you can create one by running pod init in the root of your /ios folder. Then add the following line to your pod file.</p><pre>pod &#39;Firebase/Core&#39;</pre><p>You will also need to run pod install in the root of your /ios folder</p><p>The final step in the setup process is to modify the AppDelegate.swift in the /ios/Runner folder.</p><ol><li>Add import Firebase to your list of imports</li><li>Add FirebaseApp.configure() to the body of your function</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WG8ukOJyZKJ9RHAvG3g0Hw.png" /><figcaption>AppDelegate.swift</figcaption></figure><blockquote>The order matters, so make sure you add FirebaseApp.configure() before GeneratedPluginRegistrant.register(with: self) .</blockquote><p>Now if you run your application, firebase should verify that your app has connected with it. If it hasn’t you may need to uninstall and reinstall your app.</p><p>Once all this is done, make sure to visit your database on the firebase console and initialize it. I started it off with the following rules, which allow anyone to access the database. This makes it easier to get started, but make sure to secure your database rules before pushing it out to production.</p><pre>rules_version = &#39;2&#39;;<br>service cloud.firestore {<br>  match /databases/{database}/documents {<br>    match /{document=**} {<br>      allow read, write;<br>    }<br>  }<br>}</pre><h3>Recap</h3><p>Finally, we move on to some Flutter development! To quickly recap I cleaned up the code from the last tutorial and isolated the EditDialog component into its own file, as seen below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/791fce46e337dc87c28d23da69264c65/href">https://medium.com/media/791fce46e337dc87c28d23da69264c65/href</a></iframe><h3>Flutter Setup</h3><p>Now that we have registered with Firebase and caught up with the current state of our app, we need to create a service to handle our Firebase operations.</p><p>To get started, we need to add cloud_firestore: ^0.13.4 as a dependency in our pubspec.yaml file, located at the root of our project. Make sure to run flutter pub get to install the package.</p><h3>Create a Task</h3><p>Next up, I have created a file called database.dart located in the services folder /lib/services/database.dart . I have added a function to create a task.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dafc29a3608abfba70e42fd312ff315b/href">https://medium.com/media/dafc29a3608abfba70e42fd312ff315b/href</a></iframe><p>The addTask function, creates a new document record in the tasks collection of our Firestore database. You do not have to create this collection, as Firebase will create it for us if it does not already exist.</p><p>From the previous tutorial, we have already built the UI components necessary to add a new task. Instead of adding it to a List , we will be uploading it to the database using our new database service.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f83e4910f71d037fab5de1a504954ecb/href">https://medium.com/media/f83e4910f71d037fab5de1a504954ecb/href</a></iframe><ol><li>_displayDialog returns our EditDialog component</li><li>_handleDialogSubmission creates our task and adds it to the database</li><li>_getTasks is currently a placeholder function. In the next part I will show you how to implement reading from our database.</li></ol><p>You can run the app now and add a task. It will be visible in your firebase console as seen below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZXCwK8goBi0w2lmEr5xaVw.png" /><figcaption>Firebase console displaying added task</figcaption></figure><h3>Reading Tasks</h3><p>To read our data from Firestore, we are going to be looking at the StreamBuilder widget.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f193a8f909cf106aaaa63127d7b3cb12/href">https://medium.com/media/f193a8f909cf106aaaa63127d7b3cb12/href</a></iframe><ol><li>The StreamBuilder widget will listen to events flowing from the Stream and rebuild itself for every new event. In our case we have provided it with a stream from Firestore.</li><li>The builder function allows you to construct your UI based on data from the stream.</li><li>I’ve added a simple check to see if our stream has any data and then I am generating a Listview with a simple Text widget. Later on in this tutorial, I will be creating a separate widget to encapsulate a task.</li></ol><p>You can now run the app to add tasks, and see them load in real-time.</p><h3>Updating &amp; Deleting</h3><p>The final two features we are going to add to our application are Update &amp; Delete . Just like before, we will start with updating our database.dart file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e068c65a0f0e68a6cc25df38c975b85e/href">https://medium.com/media/e068c65a0f0e68a6cc25df38c975b85e/href</a></iframe><p>Now we need to redefine our UI to accommodate the new functionality. I went ahead and created a widget to display our tasks.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/954a6f72e12bbaa78709d4d55af09829/href">https://medium.com/media/954a6f72e12bbaa78709d4d55af09829/href</a></iframe><p>There are a few key things to take away from this new widget</p><ol><li>I have defined callback functions to trigger the update and delete callbacks in main.dart</li><li>We will be needing the id of our documents when updating and deleting them. They will be passed in via the StreamBuilder component</li><li>I converted our Text widget, into a TextField . This will allow us to easily change our tasks and update them. I have done this for the sake of simplicity, however you can reuse the EditDialog and achieve the same results.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/1*tNRiAbPGH9sD0cKxbxYtAA.png" /><figcaption>Updated UI with task.dart</figcaption></figure><p>The final step in this tutorial is to update our StreamBuilder widget.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c075736c77708f7c1ceb12631e6806a/href">https://medium.com/media/6c075736c77708f7c1ceb12631e6806a/href</a></iframe><ol><li>The Text widget has been replaced with our new Task widget.</li><li>We are passing in the content and documentID to our widget, as well as the update and delete callbacks.</li><li>We are calling our database functions in the Task widget callbacks.</li></ol><h3>Summary &amp; Next Steps</h3><p>Putting all this together, you should be able to run your application with a fully functioning <strong>to-do </strong>list.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YjDiJD2-OJCKUTqFzNepvQ.gif" /></figure><p>In this tutorial, I have demonstrated the ease of connecting your Flutter application to Firebase Firestore. You can view the source code <a href="https://github.com/Xenon-Labs/Flutter-Development/tree/master/todo_app_firebase">here</a>. If you are interested in furthering the application, I have listed potential areas for continued development below.</p><ul><li>Use a Provider in lieu of a StreamBuilder to clean up your code</li><li>Repurpose EditDialog to edit your tasks</li><li>Leverage Firebase’s Authentication system to link tasks to user accounts</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8a7004b97d71" width="1" height="1" alt=""><hr><p><a href="https://medium.com/flutter-community/flutter-firestore-crud-operations-8a7004b97d71">Flutter &amp; Firestore CRUD operations</a> was originally published in <a href="https://medium.com/flutter-community">Flutter Community</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Simple To-Do List App in Flutter]]></title>
            <link>https://medium.com/flutter-community/simple-to-do-list-app-in-flutter-26abc50db741?source=rss-c28ec3653cf4------2</link>
            <guid isPermaLink="false">https://medium.com/p/26abc50db741</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[hybrid-app-development]]></category>
            <dc:creator><![CDATA[Yoganathan Shiv]]></dc:creator>
            <pubDate>Wed, 01 Jan 2020 13:47:40 GMT</pubDate>
            <atom:updated>2020-07-11T00:29:51.443Z</atom:updated>
            <content:encoded><![CDATA[<h3>A Simple To-Do List App — Flutter</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sPS34iF0LJg8OYWeFLlNmQ.png" /></figure><p>Flutter is a cross-platform development framework ideal for building mobile applications. Flutter was created by a team at Google and utilizes the Dart programming language. To see who else is building with Flutter you can view their showcase here: <a href="https://flutter.dev/showcase">https://flutter.dev/showcase</a>.</p><p>In this post, I will be explaining how to create a simple <strong>to-do</strong> app using flutter. If you have not installed flutter and seek assistance, there is a well documented guide here: <a href="https://flutter.dev/docs/get-started/install">https://flutter.dev/docs/get-started/install</a>. Once you have completed the setup we can get right into creating our application.</p><h3>Creating a Flutter App</h3><p>For this tutorial, I will be using Android Studio as my IDE. Flutter offers plugins for several IDE’s including Visual Studio Code &amp; IntelliJ. I will be running the app on a Pixel 2 Android Virtual Device, however it will work for iOS as well.</p><pre>flutter create todo</pre><pre>flutter create --org com.xenonlabs todo</pre><p>These commands will create a basic flutter app that can be installed on your device / emulator right away. The second command specifies an organization parameter which will setup your package name as com.xenonlabs.todo vs the default com.example.todo.</p><p>Reconfiguring the package name is challenging later on. While this step is optional, it is important to note that it will not impact any of the code we are about to write.</p><h3>Running the App</h3><p>To run the app you need to load an AVD which can easily be done in Android Studio.</p><ul><li>Open your project in Android Studio</li><li>If you do not already have an AVD you can create one from Tools &gt; AVD Manager</li><li>I have a Pixel 2 AVD running API 28. Now once your emulator is running you can simply hit the play button and watch the app install</li></ul><blockquote>Once your AVD is running you can also launch the app from the command line using <em>flutter run</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/1*hGOiK6sOB-JNhbEnHWRImQ.png" /><figcaption>Flutter Skeleton App running on a Pixel 2 AVD</figcaption></figure><h3>Programming in Dart</h3><p>At last, some coding. We are going to modify the main.dart file located in the lib directory. The templated code here is what we saw when we ran the app earlier.</p><p>Replace the code with the following snippet and then run the application.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0734c02efad359133f08c402fa81bdcf/href">https://medium.com/media/0734c02efad359133f08c402fa81bdcf/href</a></iframe><p>After running the code you can see that we now have an empty canvas with an app bar titled To-Do List. Now we want to get into creating our todo list.</p><h3>Flutter &amp; Widgets</h3><blockquote>The most important thing to note about flutter is that everything is a widget. Flutter applications are themselves just widgets. It might take some time to get used to this kind of programming but it speeds up the development process.</blockquote><p>Stateless widgets are sufficient when a widget does not need to be updated dynamically but for a <strong>to-do</strong> list application the list will be constantly modified. Consequently, we need to implement a stateful widget.</p><p>Update your existing code to match the following.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba34b60f29907e5bf23e148d9a75b06d/href">https://medium.com/media/ba34b60f29907e5bf23e148d9a75b06d/href</a></iframe><p>You may have noticed that we now have two classes now for our TodoList . This has been done to help maintain state.</p><blockquote>When flutter wants to update a widget, it will rebuild the entire widget. To save state we would need two classes, one for the widget itself and one which creates the state.</blockquote><p>So by having a separate state class we can update our todo list without losing our data.</p><p>Now we will implement our todo list functionality in the State class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a5ad92e6fd52b99ebb9e01c6ad061043/href">https://medium.com/media/a5ad92e6fd52b99ebb9e01c6ad061043/href</a></iframe><h3>Functional Breakdown</h3><p>You can now run the app and start building your list. Let’s take a closer look as to how this code really works.</p><ul><li>In the build function we have added a ListView Widget to populate the body of the Scaffold . The ListView comprises a children property that takes a list of widgets as the value.</li><li>The getItems functions iterates through our todo list titles List&lt;String&gt; _todoList and creates a ListTile widget for each title, compiles it into a list. Following this the function returns the compiled list to the ListView .</li></ul><blockquote>This function will get called every time the state is updated and the widget is recreated</blockquote><ul><li>We utilized the Floating Action Button to handle the functionality of adding items to the to-do list</li><li>It calls the _displayDialog function which displays a dialog for the user to enter the item that will be added to the _todoList</li><li>To do this we need to call the showDialog function which alters the apps state to show a dialog. It takes two parameters context and itembuilder. We pass in the BuildContext from the build function, and AlertDialog to the item builder parameter.</li><li>itemBuilder must return a type of AlertDialog and there are many variations but we will be using a simple AlertDialog, with a title and content parameter. For the content, I am passing in a text field with a TextEditingController to listen to and store the values of the editable text field. I also added action widgets CANCEL and ADD. When we cancel we pop the dialog off the stack to remove the overlay and return to the original screen.</li><li>When I add the item to the list, I wrap it in a setState function which lets the app know there has been a change in state and it will then rebuild the widget.</li></ul><h3>Result</h3><p>The final result can be seen in the demo below and the full code for the main.dart file can be found <a href="https://gist.github.com/yenvanio/140c573ceefc8f991b93f70436ad99e4">here</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Rb000OUZ_4gv8uBm-1H1ug.gif" /></figure><h3>Next Steps</h3><p>In this brief tutorial, I have demonstrated the potential of the Flutter framework. If you are interested in furthering the application, I have listed potential areas for continued development below.</p><ul><li>Use Shared Preferences to save the data locally</li><li>Use a remote database like Cloud Firestore to store the data in the cloud</li><li>Define your own theme</li><li>Implement Edit &amp; Delete functionality</li></ul><p><a href="https://www.twitter.com/FlutterComm">https://www.twitter.com/FlutterComm</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=26abc50db741" width="1" height="1" alt=""><hr><p><a href="https://medium.com/flutter-community/simple-to-do-list-app-in-flutter-26abc50db741">Simple To-Do List App in Flutter</a> was originally published in <a href="https://medium.com/flutter-community">Flutter Community</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>