<?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 Ali Yazdi on Medium]]></title>
        <description><![CDATA[Stories by Ali Yazdi on Medium]]></description>
        <link>https://medium.com/@aliyazdi75?source=rss-3c1dc53f770------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Wqo9mwJXDkraSrjETjJ55A.jpeg</url>
            <title>Stories by Ali Yazdi on Medium</title>
            <link>https://medium.com/@aliyazdi75?source=rss-3c1dc53f770------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 19:18:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aliyazdi75/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[Let’s make the Flutter Navigator 2]]></title>
            <link>https://medium.com/flutter-community/lets-make-the-flutter-navigator-2-bc5953251c3e?source=rss-3c1dc53f770------2</link>
            <guid isPermaLink="false">https://medium.com/p/bc5953251c3e</guid>
            <category><![CDATA[application]]></category>
            <category><![CDATA[flutter-community]]></category>
            <category><![CDATA[application-development]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[flutter-web]]></category>
            <dc:creator><![CDATA[Ali Yazdi]]></dc:creator>
            <pubDate>Wed, 14 Apr 2021 19:11:35 GMT</pubDate>
            <atom:updated>2022-01-02T09:25:58.573Z</atom:updated>
            <content:encoded><![CDATA[<p>Do you know Flutter web became stable? Do you want to support Flutter web for your app? Are you struggling to support URL navigation like a real web app? This is your ticket to support navigator 2 for your awesome application.</p><figure><img alt="Twisted Highway Routes" src="https://cdn-images-1.medium.com/max/1024/1*JA4DXAIW1EXbq4T_3J-SXA.jpeg" /><figcaption>Bunker Hill, California, United States, Photo by <a href="https://www.flickr.com/photos/shabdro/albums">Shabdro Photo</a></figcaption></figure><p>A real appreciation for <a href="https://medium.com/u/ad15395d3ce">Chun-Heng Tai</a> who helped me by reading this article and checking its <a href="https://github.com/aliyazdi75/gallery/discussions/5#discussion-3307053">quality</a>.</p><blockquote><em>This is one of the amazing features of the Gallery, so if you haven’t read about my previous articles for Gallery, you can start your journey </em><a href="https://medium.com/flutter-community/the-software-engineering-approach-in-cross-platform-programming-with-flutter-part-1-efcdc8a8fc26"><em>here</em></a><em>.</em></blockquote><p>This is a long article because I gathered all information that you may need to know about navigator 2 implementation. Just reading this article may be useless, so try to make a new project and implement it step by step like this and don’t forget to bring some snacks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/1*6hi_iT6wtB7puTcOattVgQ.gif" /></figure><p>Let’s first see the results:</p><figure><img alt="Sample navigator 2 in Browser" src="https://cdn-images-1.medium.com/max/800/1*SEFFwrvUNifKl8H46jjWNA.gif" /><figcaption>Sample navigator 2 in Browser</figcaption></figure><p>I hope you enjoyed my selected photos!</p><blockquote><em>I love everything about flutter, its people, events, communities. That&#39;s<br>why I made this gallery to show how lovely they are. I love to add your requested albums to make this Gallery more beautiful. If you want to that check this </em><a href="https://github.com/aliyazdi75/gallery-assets"><em>repository</em></a><em>.</em></blockquote><p>As you can see, our gallery supports many situations:</p><ul><li><strong>Imperative Navigation</strong></li><li><strong>Declarative Navigation</strong></li><li><strong>Possible and Impossible 404 Navigation</strong></li><li><strong>Browser Back Button Navigation</strong></li><li><strong>Browser Forward Button Navigation</strong></li></ul><p>That’s so cool, that’s not many, that’s all situations! Let’s learn them all together.</p><h3>Declarative Vs. Imperative Navigation</h3><p>I’m sure most of you are acquainted with <a href="https://flutter.dev/docs/get-started/flutter-for/declarative">declarative</a> UI programming as always we use in the Flutter widget, for example, to define a widget called ViewB you tell the framework:</p><pre>// Declarative style<br>return ViewB(<br>  color: red,<br>  child: ViewC(...),<br>)</pre><p>But for the imperative way:</p><pre>// Imperative style<br>b.setColor(red)<br>b.clearChildren()<br>ViewC c3 = new ViewC(...)<br>b.add(c3)</pre><p>Our gallery has two main screens that we want to present to the users: AlbumPage and MediaFullscreen. It also has a RootPage which actually is our first album page.</p><p>I made this simple for navigation by the following GIF:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SEc7ITyIzqcDqbTi1uuNQA.gif" /><figcaption>Declarative Vs. Imperative Navigation</figcaption></figure><p>The first boy wants to find his friend’s photo, but he doesn’t know which album it belongs to, so he should start to ask each dog that represents the gallery album.</p><p>Our second ssssweeet boy! has some information about his friend’s photo and tells the first dog, and he answers the specific address of album that photo belongs to it. Therefore he shall not waste his time and goes directly to that album.</p><p>The first boy does the same thing as always we do to the mobile application. We start our application from scratch and navigates to other screens page by page. However, things may be different in the web application, and we should handle them by Navigator 2.</p><h3><strong>Why Navigator 2?</strong></h3><blockquote>The Navigator 2.0 API adds new classes to the framework in order to make the app’s screens a function of the app state and to provide the ability to parse routes from the underlying platform (like web URLs).</blockquote><p>Navigator 2 introduces some new methods (you can read them all in this <a href="https://medium.com/flutter/learning-flutters-new-navigation-and-routing-system-7c9068155ade">article</a>) that make navigation handling harder but not complicated.</p><p>It’s so important that you don’t make your application complicated for some new features by adding some new packages. That’s why I love Flutter in this way by providing you many tools that you can support new features by using them, and your application remains easy to understand.</p><p>In this article, I will explain these:</p><ul><li><strong>Gallery Router Service</strong>: Defining Routes and Router State Models, Making Parser and State</li><li><strong>Gallery Router Delegates</strong>: Making Outer and Inner Routers</li><li><strong>Gallery Blocs</strong>: How to changing router states based on their Blocs events</li></ul><p>So, Let’s get started.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FLn2dAW9oycjgmTpjX9%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FLn2dAW9oycjgmTpjX9%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FLn2dAW9oycjgmTpjX9%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="219" frameborder="0" scrolling="no"><a href="https://medium.com/media/99f85ac3950330e5bf08bd9512adc088/href">https://medium.com/media/99f85ac3950330e5bf08bd9512adc088/href</a></iframe><h3>Gallery Navigation Structure and Data Models</h3><p>Up to now, we are acquainted with our demands and desires for our cross-platform application, and now we can jump into implementing this amazing feature. If you’ve read my previous articles mentioned before, we need to define a new service called routers_service(If you don’t wish to read them, make all classes where you want), we do so by following golden commands:</p><pre>$ cd services<br>$ mkdir routers_service<br>$ cd routers_service<br>$ flutter pub global run stagehand package-simple</pre><p>We need to define a simple model for the browser state, so we need model dependency and the gallery_service for our gallery models, but we don’t need Bloc dependency here since we can handle our routers state changes by ChangeNotifiereasily, but I will use Bloc in the presentation layer to change the routers state. Its dependencies look like this:</p><pre>dependencies:<br>  flutter:<br>    sdk: flutter<br>  model_dependency_service:<br>    path: ../dependencies_service/model_dependency_service<br>  gallery_service:<br>    path: ../gallery_service</pre><p>At last, don’t forget to add this service to your application by adding it to the pubspec.yaml:</p><pre>dependencies:<br>  routers_service:<br>    path: services/routers_service</pre><h4>Defining Routes</h4><p>This service is responsible for routers&#39; changes and defining all routes that we have in the gallery and parse them to the presentation layer. So we have three files there states, routes and parser. The routes extend from an abstract class called GalleryRoutePath, and they carry all things you should know about the route path. For example, one of the fullscreen paths <a href="https://github.yazdi.dev/gallery/#/album/2f616c62756d32?view=2f616c62756d322f70686f746f372e6a7067">/gallery/album/album2?view=photo8</a> tells our application that a client wants to go to an album that is ‘album2’ and see a photo that is ‘photo8’, so MediaFullscreenPath should contain all of them:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/99cd2b3e5b2e8ab091f375fc00b61c11/href">https://medium.com/media/99cd2b3e5b2e8ab091f375fc00b61c11/href</a></iframe><p>For the rest of the routes, we do the same. You can find them <a href="https://github.com/aliyazdi75/gallery/blob/main/services/routers_service/lib/src/routers/src/routes.dart">here</a>.</p><h4>How to make Parser &amp; Browser State?</h4><p>We will make a parser, a one-direction bridge from an operating system to our application presentation layer. Thanks to the RouteInformationParser class, not only we have the requested URI, but also we can pass navigationstate and get them back from the browser for the next route. Before making the parser, let’s define this browser state.</p><figure><img alt="Gallery Pages Structure" src="https://cdn-images-1.medium.com/max/587/1*Z_n_WGSsO72MpvUGjMlpIA.png" /><figcaption>Gallery Pages Structure</figcaption></figure><p>The browser state uses for imperative navigation and should contain all things you need to make all pages from start to the current route, as you can see in the structure of the page in the gallery ‘album’ uses for album path and ‘photo’ uses for fullscreen photo page, for example, to show ‘photo8’ photo page, we need to have all album pages underneath, so we need a list of gallery paths that we visited so far and the current album or photo. The current album or photo could be null if they’ve gotten by URL and not null if they’ve gotten from the previous album page.</p><p>To make this clear, if we are in an album, so we’ve gotten album or photo details. Otherwise, we don’t have them like by getting a URL from the browser, so they are null. Let’s make a conclusion in this model:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e61579ca71e5d16ba7b4712befb2e00/href">https://medium.com/media/2e61579ca71e5d16ba7b4712befb2e00/href</a></iframe><p>Remember you have to pass BrowserState in json to the browser, so you need both toJson and fromJson functions to work with this model.</p><h4>How should the Data Model look like?</h4><p>To provide data for the state model and album and photo details, you can get them from API. I built two API providers, server and Github contents API. You can choose whatever you want. In this article.</p><blockquote><strong><em>Notice:</em></strong><em> I will be using the Github API as a provider to get images from the </em><a href="https://github.com/aliyazdi75/gallery-assets"><em>gallery-assets</em></a><em> repository. And in the browser, you will see a hex string like </em><em>2f616c62756d32 instead of </em><em>album name, because I need to get a full path of the album something like </em><em>/album2/album3/image.jpg. But Don’t worry, I will use </em><em>album and </em><em>photo keyword in this tutorial to don’t make things ambiguous. You can check all sample responses </em><a href="https://github.com/aliyazdi75/gallery-assets/tree/main/assets/sample_responses"><em>here</em></a><em>.</em></blockquote><p>If you remember, we need two more demands from the gallery, which are:</p><ul><li><strong>Browser Back Button Navigation</strong></li><li><strong>Browser Forward Button Navigation</strong></li></ul><p>Therefore, we should alter the Gallery Pages Structure to this:</p><figure><img alt="Final Gallery Pages Structure" src="https://cdn-images-1.medium.com/max/587/1*KH0EDBz2KNphAGzoDWYkKQ.png" /><figcaption>Final Gallery Pages Structure</figcaption></figure><p>So you can conclude that we need both parents’ and children&#39;s albums for each album. Let’s look at the gallery album model:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3a3ed9bedbfd7d72a89f964d934020c/href">https://medium.com/media/a3a3ed9bedbfd7d72a89f964d934020c/href</a></iframe><p>In this model, I used the <strong>Gallery </strong>keyword for the current album and <strong>Album </strong>for its children album to distinguish between them, so don’t confuse.</p><p><strong>Why do we need to get the current path?</strong> Because to start our application, the user and our application don’t know the first occurrence of the Root album, so we need to get it from the server or provider to save it in our gallery&#39;s history, and parent is nullable since we don’t have a parent for the Root album.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/103050d966467ca5b28dd086ffa1fbfe/href">https://medium.com/media/103050d966467ca5b28dd086ffa1fbfe/href</a></iframe><p>We should do the same thing for the media or photo model. We need a property called path to get the information from the provider by this path.</p><p>To summarize it, you need two main Endpoints to make all things work:</p><ul><li>Gallery details Endpoint Provider</li><li>Photo details Endpoint Provider</li></ul><p>So if you need to make back functionality, you have to make sure to be compatible with your backend provider.</p><h3>Gallery Navigation Parser</h3><p>We should build all those stuff to make this parser. We need to extend from RouteInformationParser and override two main functions:</p><ul><li>Use parseRouteInformation to get URI and browser state from the operating system.</li><li>Use restoreRouteInformation to pass URI and browser state to the operating system.</li></ul><h4>Parse Route Information</h4><p>Definitions are obvious, and their implementations are super easy but with one trick! First, take a look at the gallery parseRouteInformation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d905c2fc01b487b6c47440b3abe65a49/href">https://medium.com/media/d905c2fc01b487b6c47440b3abe65a49/href</a></iframe><p>I divided URI into three main segments by their length and two segments for two main pages: Album and Fullscreen Photo. We should return the gallery path and state, in a wrapper class called RouterConfiguration for each segment using SynchronousFuture because the result can be computed<br>synchronously. This RouterConfiguration class is used by the presentation router delegate that I explain later.</p><p><strong>What is the trick here? </strong>The trick is that we should provide the browser state data by two means: here and from the presentation layer. Because for declarative navigation, we need the browser state data, and this function is all we have from URI, for example, album and photo paths. Be careful to do so. For example, if we have a request from the user that needs to navigate to Album and Fullscreen Page, we should do the following instructions to do that:</p><pre><em>final </em>albumPath = uri.pathSegments[1];<br><em>final </em>filePath =<br>   uri.queryParameters[MediaFullscreenPath.<em>kFullscreenPageLocation</em>];<br><em>final </em>newState = browserState.galleriesHistory.contains(albumPath)<br>    ? browserState<br>    : browserState.rebuild((b) =&gt; b..galleriesHistory.add(albumPath));</pre><p>Be careful to check that weather if this state was added to the browser state in the presentation layer before or not, by this code: browserState.galleriesHistory.contains(albumPath).</p><p>One other noticeable thing is that an unknown page should be returned if none of the paths are acceptable:</p><pre><em>// 404<br>return </em>SynchronousFuture&lt;RouterConfiguration&gt;(<br>    RouterConfiguration(<em>const </em>UnknownPagePath(), browserState));</pre><p>This is what I called <strong>Impossible 404 Navigation</strong> since the URI is not acceptable at all, something like /gallery/my/albums/album2/hello/, for example! For the possible one, I explain that later because it should be handled in the presentation layer.</p><h4>Restore Route Information</h4><p>The next method restoreRouteInformation, must return the RouteInformation that contains location which is the specific location path and the state which is our browser state, and be careful to pass it to json. The result is this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/39ef97974eb2b1f5c505f39d693ff8f2/href">https://medium.com/media/39ef97974eb2b1f5c505f39d693ff8f2/href</a></iframe><h3>Gallery Navigation Routers State</h3><p>This class should be used for presentation layer state handling, which means it holds the same BrowserState and notifies the presentation layer whenever it is changed by the presentation layer.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0dfd26b176594ed98f2974734c4467f0/href">https://medium.com/media/0dfd26b176594ed98f2974734c4467f0/href</a></iframe><p>The routePath property is the current application path that could be one of our routes, such as UnknownPagePath, LoginPagePath, RootPagePath, AlbumPagePath and MediaFullscreenPath. We should change this whenever the application path type needs to change.</p><p>OK, our router service has been finished. Now we are ready to jump into the presentation layer and handle presented pages to our lovely users.</p><h3>Gallery Navigation Presentation Layer</h3><p>In this layer, we should use RouterDelegate which is provided by the Flutter framework. As I always say, we have to make our application easy to understand, and because handling all pages at the same time in one class makes it difficult to figure it out, isn’t it? So, let’s divide our gallery presentation router into two separate RouterDelegate, <strong>Outer Router </strong>for our general-purpose pages like LoginPage, and <strong>Inner Router</strong> for RootPage, AlbumPage,MediaFullscreen and UnknownPage which all are used for the gallery screens. But skipping the simplicity,</p><h4>Why Nested Routing?</h4><ul><li>As mentioned in the <a href="https://github.com/flutter/uxr/issues/35">flutter/uxr#35</a>, this is useful for multiple teams working on the same project, and structuring in a hierarchical structure way, this makes the project less complex. For example, in the Gallery, we have two main simple screens: <a href="https://github.com/aliyazdi75/gallery/blob/main/lib/presentation/screens/login/login.dart">LoginPage</a> and <a href="https://github.com/aliyazdi75/gallery/blob/main/lib/presentation/screens/gallery/gallery.dart">GalleryShell</a>, as they aren’t related to each other, they are separated in their directory presentation layer.<br>The GalleryShell has some subpages:<br> — <a href="https://github.com/aliyazdi75/gallery/blob/da68d1741f377b4820e55a1b1834a54f5952111f/lib/main.dart#L158">RootPage</a> (the first album page),<br> — <a href="https://github.com/aliyazdi75/gallery/blob/main/lib/presentation/screens/album/view/album.dart">AlbumPage</a><br> — <a href="https://github.com/aliyazdi75/gallery/blob/main/lib/presentation/screens/fullscreen/fullscreen.dart">FullscreePage</a></li><li>We should consider changing our states as the related changes in the onPopPage method, so bringing all changes in one router which they aren’t related to each other is not a good idea. For example, this is the <a href="https://github.com/aliyazdi75/gallery/blob/da68d1741f377b4820e55a1b1834a54f5952111f/lib/presentation/screens/gallery/router.dart#L81">onPopPage</a> method for the GalleryShellwhich handles the state changes for its subpages.</li><li>Like the second reason, we sometimes need to define some observers like the HeroController, and this observer relates to the GalleryShell subpages and we have to define it just for that router. For example, this is the <a href="https://github.com/aliyazdi75/gallery/blob/da68d1741f377b4820e55a1b1834a54f5952111f/lib/presentation/screens/gallery/router.dart#L37">observers</a> for the GalleryShell.</li><li>We can simply add the specific widgets for each router that we need. For example, if we want to change the text scale of the GalleryShell subpages, we can simply wrap GalleryShell by this widget instead of wrapping every subpage with this widget:</li></ul><pre>MediaQuery(<br>   data: MediaQuery.of(context).copyWith(<br>     textScaleFactor: 4.0,<br>   ),<br>   child: GalleryShell(),<br>);</pre><p>I think these are good reasons to make the nested routers at the beginning for a big project.</p><p>‌So the outer router one should be located at <a href="https://github.com/aliyazdi75/gallery/tree/main/lib/presentation/routers">/lib/presentation/routers</a> and the inner router should be located at <a href="https://github.com/aliyazdi75/gallery/tree/main/lib/presentation/screens/gallery">/lib/presentation/screens/gallery</a>.</p><p>If you have noticed, I could use UnknownPagein general-purpose pages, but I didn’t because a possible 404 happens when we get the data from a server provider, so we should handle it in the gallery Bloc as mention later.</p><h4>Outer Router Delegate</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/1*mT-gaTSYSBRwoyHvOmBkdg.png" /><figcaption>Gallery Navigation Structure</figcaption></figure><p>This is the galley navigation structure with outer and inner structures. We built RouteInformationParser and GalleryRoutersState up to now and remains some little steps.</p><p>As you can see, the Inner router interacts with <strong>Gallery Shell</strong>. The outer router should behave with this gallery shell just like another page. Let’s see the outer gallery router class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1af811c378379ef5c1061b0db610a996/href">https://medium.com/media/1af811c378379ef5c1061b0db610a996/href</a></iframe><p>First, it should listen to any changes that come from routerState and override some methods:</p><ul><li>currentConfiguration that just returns RouterConfiguration based on routerState when route information may have changed.</li><li>setNewRoutePath that should assign routerState data comes from routeInformationProvider.</li><li>build that returns Navigator with its pages. This is where we should define our pages based on routerState. Here simply if the path is LoginPagePath pages contain LoginPage otherwise. GalleryShell.</li></ul><p><strong>How to connect the outer router to the application?</strong> Simply by changing MaterialApp to the <a href="https://github.com/aliyazdi75/gallery/blob/58574c6c736accd04e5c1a37d42660184dcf5734/lib/main.dart#L90">MaterialApp.router</a> and add <a href="https://github.com/aliyazdi75/gallery/blob/58574c6c736accd04e5c1a37d42660184dcf5734/lib/main.dart#L108">routerDelegate</a> and <a href="https://github.com/aliyazdi75/gallery/blob/58574c6c736accd04e5c1a37d42660184dcf5734/lib/main.dart#L109">routeInformationParser</a>.</p><h4>Inner Router Delegate</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a9b81995b66db593ac37eb4bd1b6aa4d/href">https://medium.com/media/a9b81995b66db593ac37eb4bd1b6aa4d/href</a></iframe><p>This is the heart of navigation, where we should define pages based on routerState. The situations are obvious:</p><ul><li>if the request is 404, just show the 404 page,</li><li>otherwise if the RootPagePath is requested or one of the galleries that the user has seen before was RootPagePath the first page is RootPage</li><li>If the request is either AlbumPagePath or MediaFullscreenPath, for each album that the user has seen, we need pages underneath</li><li>And at last, if the request is MediaFullscreenPath we have a MediaFullscreenPage that shows the photo on top of all album pages</li><li>This is where you should add your page transition animation by adding the following widget. This animation is a combination of FadeThroughTransitio and FadeTransition using animationpackage. This makes your web application more elegant and smooth in page transition. You can test this animation on the <a href="https://aliyazdi.tech/gallery">gallery</a>.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6ef38276dcc74b2d4fa2d1b3180211d7/href">https://medium.com/media/6ef38276dcc74b2d4fa2d1b3180211d7/href</a></iframe><p><strong>How to connect the outer router to the application?</strong> Simply by wrapping <a href="https://github.com/aliyazdi75/gallery/blob/58574c6c736accd04e5c1a37d42660184dcf5734/lib/presentation/screens/gallery/gallery.dart#L47">GalleryShell.build</a> with <a href="https://github.com/aliyazdi75/gallery/blob/58574c6c736accd04e5c1a37d42660184dcf5734/lib/presentation/screens/gallery/gallery.dart#L53">Router</a> and add inner <a href="https://github.com/aliyazdi75/gallery/blob/58574c6c736accd04e5c1a37d42660184dcf5734/lib/presentation/screens/gallery/gallery.dart#L54">routerDelegate</a>.</p><h3>Adding to the Bloc &amp; UI</h3><p>Finally, we reached the very last step to make this navigation successful.</p><p>We need three more states for the gallery album Bloc: successPushed, successPopped and notFound. As we planned before, we need to go back or forward in any way, imperative or declarative. So when we hit the application back button or hit the album button, if we are in an imperative way, we have our information. If not, we don’t have, and we should request gallery Bloc to get the previous or next album for both pop and push. If the server returns a 404 status code, we should trigger a not-found state.</p><p>First, we need two more events:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f463062cb576019940aae6e92d052e35/href">https://medium.com/media/f463062cb576019940aae6e92d052e35/href</a></iframe><p>then, call push event to the Bloc like by clicking on the album button:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f5f5a1155f9a4008eea88c154f428c73/href">https://medium.com/media/f5f5a1155f9a4008eea88c154f428c73/href</a></iframe><p>and then, for the push event in the gallery Bloc, we have to get the new album information in Bloc:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/59790d6fa3c3675d5117f10c3138a3e7/href">https://medium.com/media/59790d6fa3c3675d5117f10c3138a3e7/href</a></iframe><p>We need the same thing for the pop Event. If you remember, we need another 404 navigation that I called <strong>Possible 404 Navigation</strong>. This is where possible 404 happens with something like this for example: <a href="https://github.yazdi.dev/gallery/#/album/post_malone?view=sunflower">/gallery/album/post_malone?view=sunflower</a></p><p>At last, you should handle state changing with this listener in the album page:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ed717b9d6d0f0e326112c6bcec53182/href">https://medium.com/media/4ed717b9d6d0f0e326112c6bcec53182/href</a></iframe><p>Take a look at the code. You can figure out what I did. It’s simple, just changing the routerState with the new information and new routePath.</p><p>Let’s see:</p><ul><li>on GalleryStatus.successPushed first, we have the requested album information, and add it should be added to the galleriesHistory and then we have to check whether the album page is the RootPagePath or is AlbumPagePath (the root page is the first album page that we don’t know its path) and at the end, we can now change the GalleryRouterState with new routePath and browserState.</li><li>on GalleryStatus.successPopped we can do like before just for the new page is the previous page, if we don’t have that page our Bloc found it for us before and now we should remove the last page from galleriesHistory and add the new page.</li><li>on GalleryStatus.notFound just we need to tell the state that this is the UnknownPagePath and clean the browserState for us.</li></ul><p>That’s it. This is the hardest one of navigator example that we have the same screen for the push and pop. For other screens like FullScreenPage, you just need to handle notFound situation.</p><h3><strong>Two examples</strong></h3><h4><strong>Imperative way</strong></h4><p><a href="https://github.yazdi.dev/gallery">/gallery/</a> -&gt; <a href="https://github.yazdi.dev/gallery/#/album/2f616c62756d32">/gallery/album/album2</a> -&gt; <a href="https://github.yazdi.dev/gallery/#/album/2f616c62756d32?view=2f616c62756d322f70686f746f372e6a7067">/gallery/album/album2?view=photo8</a></p><p>galleriesHistory=[&#39;&#39;, &#39;album2&#39;] and we the request is MediaFullscreenPath so the pages are:</p><pre>RootPage or AlbumPage(Album1) -&gt; AlbumPage(Album2) -&gt; MediaFullscreenPage(Photo8)</pre><p>In this scenario:</p><ul><li>We don’t have any data for the RootPage, so the BrowserState.gallery is null and we should tell the gallery Bloc to get them by calling the GetGalleryRequested event:</li></ul><pre>//Inner Router<br>FadeAnimationPage(<br>  key: <em>const </em>ValueKey(RootPagePath),<br>  child: RootPage(<br>    albumPath: RootPagePath.<em>kRootPageLocation</em>,<br>    gallery: _routerState.browserState!.gallery, //this is null<br>  ),<br>),</pre><pre>// Album Page<br>if<em> </em>(state.status == GalleryStatus.initial) {<br>  BlocProvider.<em>of</em>&lt;GalleryBloc&gt;(context)<br>      .add(GetGalleryRequested(albumPath));<br>}</pre><ul><li>After getting the first album data, the user clicks on the Album2 button and then we should tell the gallery Bloc to get them by calling the GalleryPushRequested event and changing the GalleryRouterState in the listener and then we pass the data to its widget in Inner Router:</li></ul><pre>//Listener<br><em>final </em>newState = browserState.rebuild(<br>  (b) =&gt; b<br>    ..galleriesHistory.add(pushedGallery.current)<br>    ..gallery = pushedGallery.toBuilder(),<br>);<br>GalleryRouterStateScope.<em>of</em>(context)!<br>  ..routePath = newPath<br>  ..browserState = newState;</pre><pre>//Inner Router<br>FadeAnimationPage(<br>  key: ValueKey(galleryPath),<br>  child: AlbumPage(<br>    albumPath: galleryPath,<br>    gallery:_routerState.browserState!.gallery, //this contains data<br>  ),<br>),</pre><ul><li>Now we already have information about Album2 so we need to skip fetching data in the Bloc:</li></ul><pre>Stream&lt;GalleryState&gt; _mapGetGalleryRequestedToState(<br>    GetGalleryRequested event) <em>async* </em>{<br>  <em>if </em>(galleryRepository.gallery != <em>null</em>) {<br>    <em>yield </em>state.copyWith(<br>      status: GalleryStatus.success,<br>      gallery: galleryRepository.gallery!,<br>    );<br>    <em>return</em>;<br>  }<br>}</pre><ul><li>We already have the information about Photo8 too, so BrowserState.media has the data about the Photo8 and then we pass the data to its widget in Inner Router:</li></ul><pre>// on album button pressed<em><br>final </em>browserState =<br>      GalleryRouterStateScope.<em>of</em>(context)!.browserState!;<br>GalleryRouterStateScope.<em>of</em>(context)!<br>  ..routePath = newPath<br>  ..browserState = newState;</pre><pre>//Inner Router<br>FadeAnimationPage(<br>  key: <em>const </em>ValueKey(MediaFullscreenPath),<br>  child: MediaFullscreenPage(<br>    albumPath: (_routerState.routePath <em>as </em>MediaFullscreenPath)<br>        .albumPath,<br>    mediaPath: (_routerState.routePath <em>as </em>MediaFullscreenPath)<br>        .mediaPath,<br>    media: _routerState.browserState!.media, //this contains data<br>  ),<br>),</pre><h4><strong>Declarative way</strong></h4><p><a href="https://github.yazdi.dev/gallery/#/album/2f616c62756d32?view=2f616c62756d322f70686f746f372e6a7067">/gallery/album/album2?view=photo8</a></p><p>galleriesHistory=[&#39;album2&#39;] and we the request is MediaFullscreenPath so the pages are:</p><pre>AlbumPage(Album2) -&gt; MediaFullscreenPage(Photo8)</pre><p>In this scenario:</p><ul><li>We just need to fetch data for album2 and <a href="https://github.yazdi.dev/gallery/#/album/2f616c62756d32?view=2f616c62756d322f70686f746f372e6a7067">photo8</a>. And we don’t have any data since we don’t have any previous state, we call both AlbumPage(Album2) and MediaFullscreenPage(Photo8), so they call their events at the same time:</li></ul><pre>//Inner Router<br>//Album Page<br>FadeAnimationPage(<br>  key: <em>const </em>ValueKey(RootPagePath),<br>  child: RootPage(<br>    albumPath: RootPagePath.<em>kRootPageLocation</em>,<br>    gallery: _routerState.browserState!.gallery, //this is null<br>  ),<br>),<br>//MediaFullscreen Page<br>FadeAnimationPage(<br>  key: <em>const </em>ValueKey(MediaFullscreenPath),<br>  child: MediaFullscreenPage(<br>    albumPath: (_routerState.routePath <em>as </em>MediaFullscreenPath)<br>        .albumPath,<br>    mediaPath: (_routerState.routePath <em>as </em>MediaFullscreenPath)<br>        .mediaPath,<br>    media: _routerState.browserState!.media, //this is null<br>  ),<br>),</pre><pre>// Album Page<br>if<em> </em>(state.status == GalleryStatus.initial) {<br>  BlocProvider.<em>of</em>&lt;GalleryBloc&gt;(context)<br>      .add(GetGalleryRequested(albumPath));<br>}<br>//MediaFullscreen Page<br><em>if </em>(state.status == FullscreenStatus.initial) {<br>  BlocProvider.<em>of</em>&lt;FullscreenBloc&gt;(context)<br>      .add(FullscreenPushRequested(<br>    albumPath: widget.albumPath,<br>    mediaPath: widget.mediaPath,<br>  ));<br>}</pre><p>Now the application shows both pages to the user.</p><p><strong>Notice:</strong> In this scenario, because we don’t have any information about RoutersState, if the user wants to go to the previous album of album2 which is RootPage or AlbumPage(Album1), the Bloc should first fetch the Album1 data by the path of parent album and then navigate the user to the previous album. However, in the first scenario, since we have the information and stored them in the RoutersState we just need to Pop the page and show the previous album.</p><h3>Final Words</h3><p>This article showed you the instructions for implementing a new navigation system and how powerful the Flutter is. This tutorial was asked by you who were interested in the Gallery Project. I hope you enjoyed it and read it with a lot of energy.</p><p>Honestly, the main reason that provoked me to illustrate this with every detail stuck in my head was that I spent nearly one week reading the framework code and trying to implement it with the best possible one.</p><p>If something is missing here or something has to change, try to <a href="https://github.com/aliyazdi75/gallery/discussions/new"><strong>make a new discussion</strong></a>. I really enjoy interacting with this amazing flutter community.</p><p>The source code and web app link are below. Try to play with the <a href="https://github.yazdi.dev/gallery">web application</a> and request different URLs, and if something was wrong, <a href="https://github.com/aliyazdi75/gallery/issues/new/choose"><strong>file an issue</strong></a>.</p><p><strong>All related Navigator 2 codes for your convenience:</strong></p><ul><li><a href="https://github.com/aliyazdi75/gallery/tree/main/services/routers_service">routers_service</a>: <a href="https://github.com/aliyazdi75/gallery/blob/main/services/routers_service/lib/src/routers/src/routes.dart">routes</a>, <a href="https://github.com/aliyazdi75/gallery/blob/main/services/routers_service/lib/src/routers/src/parser.dart">parser</a>, <a href="https://github.com/aliyazdi75/gallery/blob/main/services/routers_service/lib/src/models/src/state.dart">state_model</a>, <a href="https://github.com/aliyazdi75/gallery/blob/main/services/routers_service/lib/src/routers/src/state.dart">routers_state</a></li><li><a href="https://github.com/aliyazdi75/gallery/blob/main/lib/presentation/routers/src/router.dart">outer_router</a></li><li><a href="https://github.com/aliyazdi75/gallery/blob/adfe2af3909fd54541f62769e05e0eb08fd077ec/lib/main.dart#L108">app_router_widget</a></li><li><a href="https://github.com/aliyazdi75/gallery/blob/main/lib/presentation/screens/gallery/router.dart">inner_router</a></li><li><a href="https://github.com/aliyazdi75/gallery/blob/main/lib/presentation/screens/gallery/gallery.dart">gallery_s</a>hell</li><li>gallery_push: <a href="https://github.com/aliyazdi75/gallery/blob/adfe2af3909fd54541f62769e05e0eb08fd077ec/services/gallery_service/lib/src/bloc/src/event.dart#L20">event</a>, <a href="https://github.com/aliyazdi75/gallery/blob/adfe2af3909fd54541f62769e05e0eb08fd077ec/services/gallery_service/lib/src/bloc/src/state.dart#L7">state</a>, <a href="https://github.com/aliyazdi75/gallery/blob/adfe2af3909fd54541f62769e05e0eb08fd077ec/services/gallery_service/lib/src/bloc/src/bloc.dart#L58">bloc</a>, <a href="https://github.com/aliyazdi75/gallery/blob/adfe2af3909fd54541f62769e05e0eb08fd077ec/lib/presentation/screens/album/view/album.dart#L148">usage</a>, <a href="https://github.com/aliyazdi75/gallery/blob/adfe2af3909fd54541f62769e05e0eb08fd077ec/lib/presentation/screens/album/view/album.dart#L30">listeners</a></li><li>gallery_pop: <a href="https://github.com/aliyazdi75/gallery/blob/adfe2af3909fd54541f62769e05e0eb08fd077ec/services/gallery_service/lib/src/bloc/src/event.dart#L29">event</a>, <a href="https://github.com/aliyazdi75/gallery/blob/adfe2af3909fd54541f62769e05e0eb08fd077ec/services/gallery_service/lib/src/bloc/src/state.dart#L8">state</a>, <a href="https://github.com/aliyazdi75/gallery/blob/adfe2af3909fd54541f62769e05e0eb08fd077ec/services/gallery_service/lib/src/bloc/src/bloc.dart#L78">bloc</a>, <a href="https://github.com/aliyazdi75/gallery/blob/adfe2af3909fd54541f62769e05e0eb08fd077ec/lib/presentation/screens/album/view/album.dart#L93">usage</a>, <a href="https://github.com/aliyazdi75/gallery/blob/adfe2af3909fd54541f62769e05e0eb08fd077ec/lib/presentation/screens/album/view/album.dart#L46">listeners</a></li><li>gallery_not_found:<a href="https://github.com/aliyazdi75/gallery/blob/adfe2af3909fd54541f62769e05e0eb08fd077ec/services/gallery_service/lib/src/bloc/src/state.dart#L9">state</a>, <a href="https://github.com/aliyazdi75/gallery/blob/adfe2af3909fd54541f62769e05e0eb08fd077ec/services/gallery_service/lib/src/bloc/src/bloc.dart#L69">bloc</a>, <a href="https://github.com/aliyazdi75/gallery/blob/adfe2af3909fd54541f62769e05e0eb08fd077ec/lib/presentation/screens/album/view/album.dart#L67">listeners</a></li><li>fullscreen_push: <a href="https://github.com/aliyazdi75/gallery/blob/adfe2af3909fd54541f62769e05e0eb08fd077ec/services/fullscreen_service/lib/src/bloc/src/event.dart#L11">event</a>, <a href="https://github.com/aliyazdi75/gallery/blob/adfe2af3909fd54541f62769e05e0eb08fd077ec/services/fullscreen_service/lib/src/bloc/src/state.dart#L3">state</a>, <a href="https://github.com/aliyazdi75/gallery/blob/adfe2af3909fd54541f62769e05e0eb08fd077ec/services/fullscreen_service/lib/src/bloc/src/bloc.dart#L26">bloc</a>, <a href="https://github.com/aliyazdi75/gallery/blob/adfe2af3909fd54541f62769e05e0eb08fd077ec/lib/presentation/screens/fullscreen/fullscreen.dart#L94">direct_usage</a>, <a href="https://github.com/aliyazdi75/gallery/blob/adfe2af3909fd54541f62769e05e0eb08fd077ec/lib/presentation/screens/album/view/media/view/media_preview.dart#L79">indirect_usage</a></li><li>fullscreen_not_found:<a href="https://github.com/aliyazdi75/gallery/blob/adfe2af3909fd54541f62769e05e0eb08fd077ec/services/fullscreen_service/lib/src/bloc/src/state.dart#L3">state</a>, <a href="https://github.com/aliyazdi75/gallery/blob/adfe2af3909fd54541f62769e05e0eb08fd077ec/services/fullscreen_service/lib/src/bloc/src/bloc.dart#L36">bloc</a>, <a href="https://github.com/aliyazdi75/gallery/blob/adfe2af3909fd54541f62769e05e0eb08fd077ec/lib/presentation/screens/fullscreen/fullscreen.dart#L71">listeners</a></li></ul><p><strong>Be Safe</strong> and Stay Tuned for the next article.</p><ul><li><a href="https://github.com/aliyazdi75/gallery">aliyazdi75/gallery</a></li><li><a href="https://yazdi.dev/gallery">Gallery</a></li></ul><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=bc5953251c3e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/flutter-community/lets-make-the-flutter-navigator-2-bc5953251c3e">Let’s make the Flutter Navigator 2</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[The Software Engineering approach in Cross-Platform Programming with Flutter (Part 3)]]></title>
            <link>https://medium.com/flutter-community/the-software-engineering-approach-in-cross-platform-programming-with-flutter-part-3-34c6eff02af0?source=rss-3c1dc53f770------2</link>
            <guid isPermaLink="false">https://medium.com/p/34c6eff02af0</guid>
            <category><![CDATA[application-development]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[flutter-app-development]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[flutter-community]]></category>
            <dc:creator><![CDATA[Ali Yazdi]]></dc:creator>
            <pubDate>Tue, 26 Jan 2021 10:22:19 GMT</pubDate>
            <atom:updated>2021-12-26T08:35:38.889Z</atom:updated>
            <content:encoded><![CDATA[<p>Designing and developing the Flutter application data models and backend using the Build value and Bloc pattern.</p><p>This part continues the previous part. If you haven’t read from the first part, go to this <a href="https://medium.com/flutter-community/the-software-engineering-approach-in-cross-platform-programming-with-flutter-part-1-efcdc8a8fc26">link</a>, or if you haven’t read the second part, go to this <a href="https://medium.com/flutter-community/the-software-engineering-approach-in-cross-platform-programming-with-flutter-part-2-f7b75056102">link</a>, then come back here.</p><figure><img alt="Software Engineering with Flutter (Part 3)" src="https://cdn-images-1.medium.com/max/1024/1*fBTn5MikcY8I3EV3BmHXzw.png" /></figure><p>In this part, I will elaborate on how to build and develop services, Blocs, models, and some other cool stuff as our features.</p><h3>The Gallery Models</h3><p>We need to be able to use the models as an immutable object and easy to serialize and deserialize them. Using an immutable object has many advantages. So I’m going to use the <a href="https://pub.dev/packages/built_value"><strong>build_value</strong></a> package to do so. You can take a look at this <a href="https://medium.com/dartlang/darts-built-value-for-immutable-object-models-83e2497922d4#.48dyezxcl">article</a> to learn more about that.</p><blockquote>To build your application models, you have to take a look at the class diagram that I talked about before. Before building the models, the application class diagram should be verified by your backend team.</blockquote><p>Let’s see the gallery models. I hope you opened the first <a href="https://medium.com/flutter-community/the-software-engineering-approach-in-cross-platform-programming-with-flutter-part-1-efcdc8a8fc26">part</a> and checking the class diagram. Some of them are obvious, like Account, Authentication, Gallery, GalleryQuery, Media, MediaType, and Album.</p><figure><img alt="Authentication Class" src="https://cdn-images-1.medium.com/max/231/1*D8Ex1RY1Fy7f-rDRpWF5ww.png" /><figcaption>Authentication Class</figcaption></figure><p>The authentication class can be split into Login, LoginBody, Register, and logout. For example, The Register and Login models look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b7bfdff87fd746fa63d3f1e1c3b55bc7/href">https://medium.com/media/b7bfdff87fd746fa63d3f1e1c3b55bc7/href</a></iframe><blockquote>Don’t forget to install the <a href="https://pub.dev/packages/built_value#tools">build value snippets plugin</a> on your editor. It helps you to prevent writing build value boilerplates on your own. For example you can create your model file with its model name and then type `bvtsf` to generate all of its code, and at last create your properties of your model.</blockquote><p>If you want to get a serializer for this model, you should create serializer.dart in its models directory like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d3e9a4f9e501ed9d32f46781b54c4511/href">https://medium.com/media/d3e9a4f9e501ed9d32f46781b54c4511/href</a></iframe><p>Then all thing you have to do is running the build_runner in the authentication_service directory by these commands:</p><pre>$ cd services/authentication_service/<br>$ flutter pub get<br>$ flutter packages pub run build_runner build</pre><p>Now you’ve got your authentication models ready to use. You can do the same steps for other services.</p><p>Some of the models you might not understand are the exceptions for the HTTP requests and the responses&#39; pagination.</p><p>The HTTP exceptions are useful when we’ve got the not ok statute code for the request, and we can throw the HTTP service by these models. The HTTP service will be explained soon. The pagination model is useful if we paged our response, like the gallery&#39;s images and videos. I think the pagination model is the most tricky, and it looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/543ba5cca63631954a05b98ffdf960d2/href">https://medium.com/media/543ba5cca63631954a05b98ffdf960d2/href</a></iframe><p>And it can be used as Pagination&lt;Gallery&gt; if the Gallery response uses the pagination.</p><p>The other models of services look like the sample. You can check other services and try to practice and code some of them on your own.</p><h3>The Gallery Blocs Layer</h3><p>The most important section of development relies on developing good Blocs for your application. Before explaining the gallery Bloc, take a look at the <a href="https://bloclibrary.dev/"><strong>Bloclibrary</strong></a> documentation and make sure you understand the core concepts. Let’s create a good Bloc for the authentication service.</p><p>Each Bloc has three segments: <strong>Bloc</strong>, <strong>Event,</strong> and <strong>State</strong>. Events should be defined in the Event class, and states should be defined in the State class.</p><ul><li>For each event that happens in the UI, the UI tells the Bloc with that event.</li><li>Bloc is responsible for getting that event and changing the application states.</li><li>In this transaction, data might have been changed, and with each state change, we might have a new build.</li></ul><p>Let’s see authentication Bloc:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e82ffdd286a48453e2ee984c1fd45f88/href">https://medium.com/media/e82ffdd286a48453e2ee984c1fd45f88/href</a></iframe><p>Make sure you add the <a href="https://pub.dev/packages/equatable"><strong>equitable</strong></a> package. Equatable is an abstract class that helps you implement equality without needing to explicitly override == and hashCode.</p><p>We need to tell the Bloc package when the state has been changed and rebuild my UI, so we use it in the State class and add the properties that cause the state to change.</p><p><strong>Steps to produce Bloc:</strong></p><ul><li>The first thing you need to do is to define your state and status. Unknown, Authenticated and Unauthenticated are the states of the authentication service. If the authentication happens, an account must be saved. Put state functions in the state and extend it from equitable class, then override props and put the properties you want to differentiate between states. We will use this ability in the UI.</li></ul><ol><li>The events should extend from an abstract class that extends equitable. So I made AuthenticationEvent and the AuthenticationStatusChanged extends of that.</li><li>The last thing is defining the Bloc class and overriding the mapEventToState method, mapping the event, making your desired functionality, and changing the state by yielding the new state. I separate the login action from the authentication because the authentication can happen anytime. However, the login just happens when the app start and the user has been unauthenticated.</li></ol><p>For the other Blocs, the same thing should be considered:</p><pre><strong>Defining the Bloc states</strong> -&gt; <strong>Defining the Bloc events that happen in the UI </strong>-&gt; <strong>Defining the Bloc map events to states</strong>.</pre><h3>The Gallery Data Layer</h3><p>As we planned, I’m going to split this layer into the repository and the providers for each Bloc. Let’s take a look at the authentication repository:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e547fd85d5a64d1e3cb1d828ef2181b/href">https://medium.com/media/0e547fd85d5a64d1e3cb1d828ef2181b/href</a></iframe><p>As a Bloc, I want the repository to give me the login or register model. I don’t want to know how it is going to do that. The responsibility of giving the login or register model by any means is for the authentication repository.</p><blockquote>The authentication repository first could ask the storage provider for any authenticated account if the storage doesn’t have one, then the repository calls the API provider, and the API provider calls the HTTP service and provides the account login or register model.</blockquote><p>I haven’t implemented the storage provider yet, but the API provider looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3de472d1750cc46bd00913c14b14e0b8/href">https://medium.com/media/3de472d1750cc46bd00913c14b14e0b8/href</a></iframe><p>I will explain the HTTP service later, but we just know that we need to pass the URL and method and the body and then get the response body from it if the HTTP exceptions have not occurred.</p><h3>The Gallery Presentation Layer</h3><p>Finally, this is the layer all things come true, and we can see the efforts that we made.</p><p>This layer is located in the lib/presentation/screens directory. We have a separate directory for each screen we want to make, and the general widgets should be located in the lib/presentation/widgets directory.</p><h4><strong>What is happening between layers in a nutshell:</strong></h4><ol><li>Something happens in the UI.</li><li>UI tells the Bloc with the defined event.</li><li>Bloc observes that event and requests its repository for data if needed (If Bloc doesn’t need the repository, go to step 7).</li><li>The repository asks the provider to provide the data.</li><li>The provider uses specific services that collect the data.</li><li>The data return from services to the provider, then to the repository, then to the Bloc.</li><li>The Bloc changes the state with data if needed.</li><li>The UI that is observing the state changes rebuilds itself with the new state data.</li></ol><p>So in order to do that in the UI layer, we should define them in reverse. It means that the RepositoryProvider should be the parent, then the BlocProvider should be the first child of the repository, the BlocListener is the widget for the UI listening, the Bloc should be the second child, and in the end the UI should be the last child, the hierarchy looks like this:</p><pre><strong>RepositoryProvider(optional) -&gt; BlocProvider -&gt; BlocListener -&gt; UI</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*45Tzia7Dob2gKlQHyWjgbA.png" /><figcaption>Authentication Activity Diagram</figcaption></figure><p>In this layer, you have to connect your presentation layer to its Bloc while you are checking its activity diagram. Make sure you are building the widgets like the above sequence. For example, the authentication presentation looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8029c86ee186ee5971010c01e6359e49/href">https://medium.com/media/8029c86ee186ee5971010c01e6359e49/href</a></iframe><p>I eliminated some unnecessary codes. As you can see, authentication is one of the most important widgets and should be the parent of the rest widgets, and if the user credit has been expired, it should handle the new state for that situation and navigate the user to the login page.</p><h3>Authentication Service Full Structure</h3><p>Let’s see what the outcome of the full structure of the authentication service is:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/24e9139283717c388b61db8d972f75f7/href">https://medium.com/media/24e9139283717c388b61db8d972f75f7/href</a></iframe><p>This is what you have to create and build for each service that needs the Bloc and data layers. The Bloc, Models, providers, and the repository.</p><h3>Final Words</h3><p>Congratulations, at this point, you understand how to make a good application. Some many features and services remain to explain as we planned. Testing and deployment phases are going to be the next article subject. The features and services will be covered beyond this series, so stay tuned and tell me what’s the best gallery feature needs to cover first.</p><p>You can check out the gallery repository, but I recommend you not to clone it. Please make your own repository and try to do all steps on your own and then share your repository with me or others in the Flutter community. And make the best community in the technology world for Flutter.</p><p>Thank you for reading. I hope you enjoyed it. This article takes me so much time researching and developing in both software engineering and flutter programming, so if you found this article helpful, please clap and share this article. If you have any suggestions to make this article better, don’t hesitate to contact me or write a comment down below and I’d reply to you fast if I weren’t asleep.</p><ul><li><a href="https://github.com/aliyazdi75/gallery">aliyazdi75/gallery</a></li><li><a href="https://yazdi.dev/">Ali Yazdi Homepage</a></li></ul><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=34c6eff02af0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/flutter-community/the-software-engineering-approach-in-cross-platform-programming-with-flutter-part-3-34c6eff02af0">The Software Engineering approach in Cross-Platform Programming with Flutter (Part 3)</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[The Software Engineering approach in Cross-Platform Programming with Flutter (Part 2)]]></title>
            <link>https://medium.com/flutter-community/the-software-engineering-approach-in-cross-platform-programming-with-flutter-part-2-f7b75056102?source=rss-3c1dc53f770------2</link>
            <guid isPermaLink="false">https://medium.com/p/f7b75056102</guid>
            <category><![CDATA[flutter-community]]></category>
            <category><![CDATA[flutter-development]]></category>
            <category><![CDATA[application-development]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Ali Yazdi]]></dc:creator>
            <pubDate>Mon, 25 Jan 2021 10:06:04 GMT</pubDate>
            <atom:updated>2021-12-26T08:35:13.421Z</atom:updated>
            <content:encoded><![CDATA[<p>Designing and building the Flutter application structure using the Bloc pattern in the micro-service architecture.</p><p>This part continues the previous part. If you haven’t read it, go to this <a href="https://medium.com/flutter-community/the-software-engineering-approach-in-cross-platform-programming-with-flutter-part-1-efcdc8a8fc26">link</a>, then come back here.</p><figure><img alt="Software Engineering with Flutter (Part 2)" src="https://cdn-images-1.medium.com/max/1024/1*CAAlVwZweLYBg3VPyBiitA.png" /></figure><p>At this point, we modelized the gallery software product. If you remember, we should turn back to the communication phase and ask the others to comment on your models and improve your models by fixing their problems. For the gallery project, I have to ask for your opinions, so if you’ve read the previous part and have any suggestions, write your comment and tell me how to make these models better.</p><h3>Construction</h3><p>Congratulations, we finally reached this phase. OK, before creating the project, make sure you are in the master channel and enable web and desktop by running these commands:</p><pre>$ flutter branch master<br>$ flutter config --enable-web<br>$ flutter config --enable-linux-desktop</pre><p>I will use and build for the Linux platform, but you can run the last command for your own platform. Now, you’re ready to create your project. You can choose the name whatever you like:</p><p>$ flutter create gallery</p><p>Let’s look at the galley project structure with its features:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c0b9204f1c807a3a9f6e52481feb3809/href">https://medium.com/media/c0b9204f1c807a3a9f6e52481feb3809/href</a></iframe><p>We decided to use Micro-Service architecture for the gallery application, so I have the services directory for the gallery services. I use the lib directory to locate gallery presentation layers and their functionality functions. Let’s explain other routes:</p><ul><li>lib/core: put some functions used for presentations.</li><li>lib/l10n: put your localizations, and use l10n.yaml to generate gallery localization.</li><li>lib/presentation/widgets: put reusable widgets here.</li><li>lib/presentation/screens: put screens here, and each screen contains its components. If we need to use small and simple Bloc and data layers for a specific screen, we don’t need to use a separate service. Instead, we can use the cubit directory for its purpose. Use this <a href="https://bloclibrary.dev/#/coreconcepts?id=cubit-vs-bloc">link</a> to learn more about Cubit.</li></ul><p>As I explained about the Monolithic architecture, if you want to use this one, you should put all Blocs in lib/bloc directory and all of the data layers models and repositories in lib/data directory. Then create a new directory for each new feature. It’s not efficient, yes? It’s so ambiguous, and we can find each feature in every directory, and if we face a problem, it will be so difficult to debug that.</p><h3>Create a new Service</h3><p>Let’s describe the gallery Micro-Service approach. We have a separate services directory, and every service inside this directory doesn&#39;t concern with each other unless we define it in its own dependency. Let’s make an example for the gallery authentication service. This is the structure of the authentication directory:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5b9e21b974d807d40af8194bb2d7a4d3/href">https://medium.com/media/5b9e21b974d807d40af8194bb2d7a4d3/href</a></iframe><p>The first thing you should know, I didn’t create all those files by hand. These are the steps for creating a service, I’m going to use the <a href="https://pub.dev/packages/stagehand">stagehand</a> package:</p><ul><li>Activate this package by this command:</li></ul><pre>$ flutter pub global activate stagehand</pre><ul><li>Make a directory with your service name like authentication_service:</li></ul><pre>$ mkdir authentication_service</pre><ul><li>Change to the new service directory:</li></ul><pre>$ cd authentication_service</pre><ul><li>Run this command to the new package as a service:</li></ul><pre>$ flutter pub global run stagehand package-simple</pre><p>It will generate a bunch of files that contain test and example directories. These are optional, and if you want to test your service or make an example for that service, you should keep them.</p><p>Program the new service and keep its files under lib/src directory, and then export those files that you want them to use as public classes. It’s useful for other services to import that file if they need this service. For example, authentication_service.dart could be like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d400f648e0904b0abc09183d871b6d16/href">https://medium.com/media/d400f648e0904b0abc09183d871b6d16/href</a></iframe><p>Don’t worry, and I will explain those Bloc and models files in the next part.</p><pre>└── src<br>    ├── bloc<br>    ├── models<br>    ├── providers    <br>    └── repository</pre><p>Authentication is one of the gallery services that needs Bloc and models. As you can see, I separated its Bloc layer and split the data layer into providers and repository (If you don’t know about them, go back to the previous part).</p><p>Let’s see what is inside of its /services/authentication/pubspec.yaml:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7584149836328435af8a639504f203f8/href">https://medium.com/media/7584149836328435af8a639504f203f8/href</a></iframe><p>I used bloc_dependency_service and model_dependency_service, since I don’t want to use Bloc and model dependencies directly inside of each service. Why? If you want to upgrade your Bloc package and add some new features to your services, you don’t need to upgrade every single service. Instead, you can upgrade its dependency service. The gallery bloc dependencies service looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ff7ee10d9a10006f03107eda73db01c/href">https://medium.com/media/4ff7ee10d9a10006f03107eda73db01c/href</a></iframe><p>And for example, /services/dependencies_service/bloc/pubspec.yaml of the bloc dependency service looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6ddb9881b74a1e6abdfd9af6b8c28552/href">https://medium.com/media/6ddb9881b74a1e6abdfd9af6b8c28552/href</a></iframe><p>Remember that if you want to use an external dependency more than once in your project, make a separate dependency and put it inside the dependencies service.</p><h3>Supporting New Features</h3><p>If you have noticed, all of Bloc dependencies support nullsafety and therefore, we can migrate this service to nullsafety by increasing the minimum version of Dart SDK to 2.12.0–0 because as the <a href="https://dart.dev/null-safety/migration-guide#step1-wait">doc</a> says:</p><blockquote>package C depends on package B, which depends on package A, then A should be migrated to null safety first, then B, then C.</blockquote><figure><img alt="Null Safety Dependencies" src="https://cdn-images-1.medium.com/max/454/0*9K6D_dvthAKF-cAy.png" /><figcaption>Null Safety Dependencies</figcaption></figure><blockquote>Although you <a href="https://dart.dev/null-safety/unsound-null-safety"><em>can</em> migrate</a> before your dependencies support null safety, you might have to change your code when your dependencies migrate. For example, if you predict that a function will take a nullable parameter but the package migrates it to be non-nullable, then passing a nullable argument becomes a compile error.</blockquote><p>You can consider this as an advantage of using services, so if some features are added to the Flutter or Dart, it would be much easier to migrate them partially. I don’t want to explain how to migrate to null safety, but you can check the <a href="https://github.com/aliyazdi75/gallery/tree/main/services/video_service">video service</a> as a null safety service. If you want me to explain how to migrate null safety, comment me, and I can make a separate story for that sake.</p><h3>Final Gallery Pubspec</h3><p>If you implement all services, you should have pubspec.yaml in your project root looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d8aa6257262befe6a259452ef5607f75/href">https://medium.com/media/d8aa6257262befe6a259452ef5607f75/href</a></iframe><p>As you can see, I used other presentation dependencies before gallery services dependencies. I don’t use the HTTP service in the gallery presentation layer, so I don’t need to add it here.</p><h3>Analysis Options</h3><p>The last thing that your project should contain is the analysis_options.yaml. It is so useful to make your own dart analyzer rules. The Dart analyzer has some optional warnings that you can consider as errors. Why? Because if you are working as a team, all your team members should code look like each other. It makes your project codes more consistent and more Integrated. Here’s the simple analysis_options.yaml:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5faeb23f525c82985da54abf993f0034/href">https://medium.com/media/5faeb23f525c82985da54abf993f0034/href</a></iframe><h3>Monolithic Structure</h3><p>Okay, that’s cool, but I don’t have much time and don’t have a complicated system. What should I do? As I mentioned, we use this architecture to easily understand our system, so there is no must. Make a Monolithic Structure for your application if you can make it. If you want to check this structure, this <a href="https://github.com/aliyazdi75/flutter_chat">repository</a> might be helpful.</p><p>For example, a Monolithic Structure could look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0c02ecf715ee0c4ecb2802eaf245e9e2/href">https://medium.com/media/0c02ecf715ee0c4ecb2802eaf245e9e2/href</a></iframe><p>In this structure, we gather all Blocs, Data Models, Repositories, and Providers in the same directory, and separate them based on each feature and the layers like lob/blocs, lib/data , lib/presentation, and we can use the services keyword just for our data providers like network and storage.</p><h3>Final Words</h3><p>I don’t want to elaborate on other service structures. They so much look like the authentication service. In the next part, I will implement the gallery application requirements that we planned for.</p><p>You can check out the gallery repository, but I recommend you not to clone it. Please make your own repository and try to do all steps on your own and then share your repository with me or others in the Flutter community. And make the best community in the technology world for Flutter.</p><p>Thank you for reading. I hope you enjoyed it. This article takes me so much time researching and developing in both software engineering and flutter programming, so if you found this article helpful, please clap and share this article. If you have any suggestions to make this article better, don’t hesitate to contact me or write a comment down below and I’d reply to you fast if I weren’t asleep.</p><p>You can go to the next part with this <a href="https://medium.com/flutter-community/the-software-engineering-approach-in-cross-platform-programming-with-flutter-part-3-34c6eff02af0">link</a>.</p><ul><li><a href="https://github.com/aliyazdi75/gallery">aliyazdi75/gallery</a></li><li><a href="https://yazdi.dev/">Ali Yazdi Homepage</a></li></ul><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=f7b75056102" width="1" height="1" alt=""><hr><p><a href="https://medium.com/flutter-community/the-software-engineering-approach-in-cross-platform-programming-with-flutter-part-2-f7b75056102">The Software Engineering approach in Cross-Platform Programming with Flutter (Part 2)</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[The Software Engineering approach in Cross-Platform Programming  with Flutter (Part 1)]]></title>
            <link>https://medium.com/flutter-community/the-software-engineering-approach-in-cross-platform-programming-with-flutter-part-1-efcdc8a8fc26?source=rss-3c1dc53f770------2</link>
            <guid isPermaLink="false">https://medium.com/p/efcdc8a8fc26</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[application-development]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[flutter-community]]></category>
            <category><![CDATA[application]]></category>
            <dc:creator><![CDATA[Ali Yazdi]]></dc:creator>
            <pubDate>Sun, 24 Jan 2021 22:07:16 GMT</pubDate>
            <atom:updated>2021-12-26T08:33:58.266Z</atom:updated>
            <content:encoded><![CDATA[<h3>The Software Engineering approach in Cross-Platform Programming with Flutter (Part 1)</h3><p>Using Software Engineering methods to design and build a fully responsive and adaptive mobile, desktop, and web application.</p><p>What you will be learning in this series:</p><ol><li><a href="https://medium.com/flutter-community/the-software-engineering-approach-in-cross-platform-programming-with-flutter-part-1-efcdc8a8fc26">What software engineering methods are and how to use them from Scratch to Hatch.</a></li><li><a href="https://medium.com/flutter-community/the-software-engineering-approach-in-cross-platform-programming-with-flutter-part-2-f7b75056102">How to design and build your Flutter application structure.</a></li><li><a href="https://medium.com/flutter-community/the-software-engineering-approach-in-cross-platform-programming-with-flutter-part-3-34c6eff02af0">How to design and develop the Flutter application data models and backend.</a></li></ol><figure><img alt="Software Engineering with Flutter (Part 1)" src="https://cdn-images-1.medium.com/max/1024/1*kosdKLBSo3rFe2WsMrxjhQ.png" /></figure><p>In the beginning, you should know some software engineering concepts.</p><p><strong>Who are you?</strong></p><blockquote>You’re a software engineer who wants to build and support a software product and virtually everyone in the industrialized world uses it either directly or indirectly.</blockquote><p><strong>What are the steps?</strong></p><blockquote>Customers and other stakeholders express the need for a software product, engineers build the software product, and end-users apply the software to solve a specific problem or to address a specific need.</blockquote><p><strong>What is the work product?</strong></p><blockquote>A computer program runs in one or more specific environments and services one or more end-users&#39; needs.</blockquote><p>If you’re accountable for your product or working in an agile team, and you can share your opinions with the rest of the team members, you have to know about these definitions and try your best to build a successful software product.</p><h3><strong>Software Lifespan</strong></h3><figure><img alt="Software Lifespan and Failure rate" src="https://cdn-images-1.medium.com/max/642/1*unyl_JkcF3Zn5SXK7LXpBw.png" /><figcaption>Software Lifespan and Failure rate</figcaption></figure><p>If we have a software product, during its life, the software will undergo change. As changes are made, it is likely that errors will be introduced, causing the failure rate curve to spike, as shown in the <strong>“actual curve”</strong>. Before the curve can return to the original steady-state failure rate, another change is requested, causing the curve to spike again. Slowly, the minimum failure rate level begins to rise — the software is deteriorating due to change.</p><p>Another aspect of wear illustrates the difference between hardware and software. When a hardware component wears out, it is replaced by a spare part. There are no software spare parts. Every software failure indicates an error in design or in the process through which design was translated into machine-executable code. Therefore, the software maintenance tasks that accommodate requests for change involve considerably more complexity than hardware maintenance.</p><blockquote><strong>Software engineering methods strive to reduce the magnitude of the spikes and the slope of the actual curve in the above Figure.</strong></blockquote><p>OK, that’s cool. Software engineering seems to be nice for our product. So what is software engineering? As I said, I’m not going to deep dive into the software engineering methods, but I will be reviewing some of its aspects and pick the best approach in cross-platform programming.</p><h3>Software Engineering</h3><p>Software engineering encompasses a process, a collection of methods, and an array of tools that allow professionals to build a high-quality software product.</p><p>As software engineers, we have to do some activities, and it is called “The Process Framework”. A generic process framework for software engineering encompasses five activities:</p><ol><li><strong>Communication.</strong> Before any technical work can commence, it is critically important to communicate and collaborate with the customer (and other stakeholders). The intent is to understand stakeholders’ objectives for the project and to gather requirements that help define software features and functions.</li><li><strong>Planning.</strong> Any complicated journey can be simplified if a map exists. A software project is a complicated journey, and the planning activity creates a “map” that helps guide the team as it makes the journey. The map — called a software project plan — defines the software engineering work by describing the technical tasks to be conducted, the risks that are likely, the resources that will be required, the work products to be produced, and a work schedule.</li><li><strong>Modeling.</strong> Whether you’re a landscaper, a bridge-builder, an aeronautical engineer, a carpenter, or an architect, you work with models every day. You create a “sketch” of the thing so that you’ll understand the big picture — what it will look like architecturally, how the constituent parts fit together, and many other characteristics. If required, you refine the sketch into greater and greater detail in an effort to better understand the problem and how you’re going to solve it. A software engineer does the same thing by creating models to understand better software requirements and the design that will achieve those requirements.</li><li><strong>Construction.</strong> What you design must be built. This activity combines code generation (either manual or automated) and the testing that is required to uncover errors in the code.</li><li><strong>Deployment. </strong>The software (as a complete entity or as a partially completed increment) is delivered to the customer, who evaluates the delivered product and provides feedback based on the evaluation.</li></ol><figure><img alt="The Process Framework Flow" src="https://cdn-images-1.medium.com/max/904/1*6S9FA9LrOG5WJIHwo_VlvQ.png" /><figcaption>The Process Framework Flows</figcaption></figure><p>As I’ll explain later, I’m going to design and program a Flutter project by the feature-driven approach. Hence, we have to accomplish these activities for each feature that drives from communication activity.</p><p>So, we have four ways to do that, and you can choose whichever fits your team. For example, if you are working in an agile team, you definitely have a stand-up meeting every day with your team, and you can discuss each feature and iterate each activity until the best construction comes up.</p><h3>Understanding Requirements</h3><p>In a big and complicated system, have you ever been wondering about some features that your boss asked of you and, you asked yourself, are they really mandatory for this system? Are they really considered as system requirements?</p><p>Before you begin any technical work, it’s a good idea to create a set of requirements for any engineering tasks.</p><blockquote>These tasks lead to an understanding of what the business impact of the software will be, what the customer wants, and how end-users will interact with the software. Designing and building an elegant software product that solves the wrong problem serves no one’s needs.</blockquote><p>That’s why it’s important to understand what the customer wants before you begin to design and build a software system.</p><p><strong>What are the steps?</strong> Requirements engineering begins with <strong>inception</strong> (a task that defines the scope and nature of the problem to be solved). It moves onward to <strong>elicitation</strong> (a task that helps stakeholders define what is required), and then <strong>elaboration</strong> (where basic requirements are refined and modified). As stakeholders define the problem, <strong>negotiation</strong> occurs (what are the priorities, what is essential, when is it required?) Finally, the problem is specified in some manner and then <strong>reviewed</strong> or <strong>validated</strong> to ensure that<br>your understanding of the problem and the stakeholders’ understanding of the problem coincide.</p><h4>Agile Requirements Elicitation</h4><p>Within the context of an agile process, requirements are elicited by asking all stakeholders to create user stories. Each user story describes a simple system<br>requirement written from the user’s perspective. User stories can be written on small note cards, making it easy for developers to select and manage a subset of requirements to implement for the next product increment. Proponents claim that using note cards written in the user’s own language allows developers to shift their focus to communication with stakeholders on the selected requirements rather than their own agenda.</p><h3>The Gallery Project</h3><p>That’s enough for just theories. Let’s dig into small projects that I want to use software engineering methods and learn within them.</p><p>I want to build a simple gallery project for our friends with Flutter that includes albums, and each album consists of pictures and videos. Since a few plugins support web applications, I decided to use the flutter video plugin for this project.</p><blockquote>I have to consider a principle for this product that the gallery application has worldwide scales users.</blockquote><p>Let’s write some use-cases that performed by the graduate actor:</p><ul><li>Request for login.</li><li>See images and videos in a list.</li><li>Select an album to change the current album.</li><li>Select an image to control zoom.</li><li>Select a video to watch and control it.</li><li>Use the gallery from Web/Mobile/Desktop.</li><li>Change the gallery language to their language.</li><li>Change the gallery theme.</li><li>Change the Web/Desktop windows dimension size.</li><li>Access to any desired album by a specific route URL.</li></ul><p>Some of its requirements are similar in many applications like multilanguage and theme support, and I will add them to the gallery.</p><blockquote>As I noted earlier, there are many ways to build a flutter application. For example, you can use just statefull widgets to control everything in your states, but do you really do that? Can you give your codes to someone else and ask them to explain your states? This is one way, and it could be the easiest and faster way, and in some cases, it could be the only way to do that, but in a complicated and world-wide scales systems, there is no answer by going to this way programming.</blockquote><h3><strong>Use-Case Diagram</strong></h3><p>In many cases, there is no need to create a graphical representation of a usage scenario. However, diagrammatic representation can facilitate understanding, particularly when the scenario is complex. I’m going to build the graphical representations with this free tool (<a href="https://app.diagrams.net/">https://app.diagrams.net/</a>). Let’s make an example for our <strong>Use-Case Diagram</strong>:</p><figure><img alt="Use-Case Diagram" src="https://cdn-images-1.medium.com/max/684/1*l0hN2rW5MQu7o351Rowy4A.png" /><figcaption>Use-Case Diagram</figcaption></figure><p>I will use these use-cases in the gallery application later. Since I made a use-case for the gallery application, not the whole system, so I considered API as an external service, and I won’t check the API server out. I will explain what a good restful API is and how much the bad API impacts our application performances.</p><h3><strong>Activity Diagram</strong></h3><p>One of the most important and mandatory for a complicated system is the <strong>Activity Diagram</strong>. The activity diagram allows you to represent the flow of activities described by the use case and at the same time indicate which actor (if there are multiple actors involved in a specific use case) has responsibility for the action described by an activity rectangle. Responsibilities are represented as parallel segments that divide the diagram vertically, like the lanes in a swimming pool. In the gallery application, I have more scenarios, and I just represent some flows of activities for simplicity:</p><figure><img alt="Activity Diagram" src="https://cdn-images-1.medium.com/max/829/1*-S7zmjxutnXou0H0QxKT9Q.png" /><figcaption>Activity Diagram</figcaption></figure><p>Seems familiar to you? Yes, I will always use this representation for the gallery application state managers. I will use this diagram for the gallery state manager later.</p><p>We could have some other types of diagrams like the <strong>State Diagram </strong>that represents active states for each class and the events(triggers) that cause changes between these active states, and the <strong>Sequence Diagram </strong>indicates how events cause transitions from object to object. They could be useful for the gallery state management, but we aren’t going to represent them for simplicity, and the activity diagram is sufficient for our app.</p><h3><strong>Class Diagram</strong></h3><p>Let’s move on to gallery models. We need a <strong>Class Diagram </strong>for this phase. I’m going to draw the classes that represent the gallery application models. Here’s the gallery class diagram:</p><figure><img alt="Class Diagram" src="https://cdn-images-1.medium.com/max/837/1*2RIg3PRkkwX0FM1elCvSHQ.png" /><figcaption>Class Diagram</figcaption></figure><p>As you can see, I have models for account and authentication and gallery. With each HTTP request, I have some models for HTTP exceptions and bad requests. I will use all of these models in the gallery application, and you will understand what the relations between them are.</p><h3>Application Architecture</h3><p>Now it’s time to design the gallery architecture. I have been acquainted with almost each state management in Flutter and used them in my professional projects. Personally, I prefer the Bloc pattern and its architecture for almost every situation since Bloc makes it easy to separate presentation from business logic, making your code <em>fast</em>, <em>easy to test</em>, <em>reusable, </em>and more important <em>predictable</em> and <em>understandable</em>. You can check Bloc <a href="https://bloclibrary.dev/">documentation</a>, but if you stay up with this series, you can learn about Bloc.</p><figure><img alt="Bloc Diagram" src="https://cdn-images-1.medium.com/max/800/1*V_TQw6xQSCdLj7elNoGVwg.png" /><figcaption>Bloc Diagram</figcaption></figure><p>I want to separate the gallery data layer from the UI layer because we can understand the gallery application super easily. And if you could accomplish that without using a state manager library, I would recommend you use just InheritedWidget or StatefullWidget, and I will check this too with a portfolio website example.</p><p>I think you understood one problem! Where’s the gallery data came from?</p><figure><img alt="Data Diagram" src="https://cdn-images-1.medium.com/max/485/1*nouPrVq3mdHwB0hZ4Arfiw.png" /><figcaption>Data Diagram</figcaption></figure><p>The data layer can be split into two parts:</p><ul><li><strong>Repository</strong></li><li><strong>Data Provider</strong></li></ul><p>This layer is the lowest level of the application and interacts with databases, network requests, and other asynchronous data sources and provides the Bloc&#39;s required data.</p><h4>Micro-Services vs. Monolithic architecture</h4><p>We have two options to use Blocs in the gallery application; Micro-Services and Monolithic:</p><figure><img alt="Micro-Services vs Monolithic architecture" src="https://cdn-images-1.medium.com/max/1024/1*kAWXqX663tFvl-JWIcpMlg.png" /><figcaption>Micro-Services vs. Monolithic architecture</figcaption></figure><p>Their difference is obvious. We aim to make the project as easy as possible. Hence, to locate every Bloc, Data and Presentation layers together in one place called a <strong>Monolithic</strong> application and, therefore, it makes our application hard to understand. So, we’re going to use the <strong>Micro-Service </strong>application. It looks like the other gallery layers don’t concern with the gallery presentation layer, and we can add any service whenever we want for any new feature and use another service by adding their dependency in their pubspec.yaml.</p><p>This architecture is more comprehensible, more reusable, more testable, more collaborative, and works amazingly for a big team and complicated systems. You don’t need to use this architecture unless your project is big and complicated. If you’re not agreed with me, first use this <a href="https://www.google.com/search?q=monolithic+vs+microservices">link</a> to do some research about them, and if you decide otherwise, there are no worries, you can use a monolithic application alongside this article.</p><h3>Final Words</h3><p>That’s enough for this part. In the next part, I will cover the gallery construction phase and try to construct the gallery application based on what we planned, and cover some other software engineering concepts for the rest of the product lifecycle.</p><p>Before starting the next part, if you are curious about alternative software engineering methods or you want to learn every detail of the methods that I covered, you can read software engineering books like Sommerville or Pressman software engineering books.</p><p>Thank you for reading. I hope you enjoyed it. This article takes me so much time researching and developing in both software engineering and flutter programming, so if you found this article helpful, please clap and share this article. If you have any suggestions to make this article better, don’t hesitate to contact me or write a comment down below and I’d reply to you fast if I weren’t asleep.</p><p>You can go to the next part with this <a href="https://medium.com/flutter-community/the-software-engineering-approach-in-cross-platform-programming-with-flutter-part-2-f7b75056102">link</a>.</p><ul><li><a href="https://github.com/aliyazdi75/gallery">aliyazdi75/gallery</a></li><li><a href="https://yazdi.dev/">Ali Yazdi Homepage</a></li></ul><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=efcdc8a8fc26" width="1" height="1" alt=""><hr><p><a href="https://medium.com/flutter-community/the-software-engineering-approach-in-cross-platform-programming-with-flutter-part-1-efcdc8a8fc26">The Software Engineering approach in Cross-Platform Programming  with Flutter (Part 1)</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>