<?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 Abdelkrim Bourennane on Medium]]></title>
        <description><![CDATA[Stories by Abdelkrim Bourennane on Medium]]></description>
        <link>https://medium.com/@abdelkrim.bournane?source=rss-908f733155fd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*-ZTLRS29zu-3UcDtK7fUYg.jpeg</url>
            <title>Stories by Abdelkrim Bourennane on Medium</title>
            <link>https://medium.com/@abdelkrim.bournane?source=rss-908f733155fd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 13:52:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@abdelkrim.bournane/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[Architecture Enforcement in Flutter Apps]]></title>
            <link>https://itnext.io/architecture-enforcement-in-flutter-apps-5618823925b7?source=rss-908f733155fd------2</link>
            <guid isPermaLink="false">https://medium.com/p/5618823925b7</guid>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[flutter-app-development]]></category>
            <dc:creator><![CDATA[Abdelkrim Bourennane]]></dc:creator>
            <pubDate>Fri, 24 Feb 2023 09:47:43 GMT</pubDate>
            <atom:updated>2023-03-02T10:17:40.234Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PmCTmD9tHFD8L2nEVxg4PA.png" /><figcaption>Flutter architecture</figcaption></figure><p>When we start writing a Flutter application (or any other language/framework for this matter), we always have an architecture in mind that we want our code to respect and follow. Different architectural styles have different objectives and each buys you different options.<br> However, when the project gets bigger and more coding hands are involved, the code can go astray, and we start diverging from our desired architecture (excluding, the case when the architecture itself shifts as part of an evolutionary design). This phenomenon is known as a model-code gap :</p><blockquote><a href="https://www.georgefairbanks.com/software-architecture/model-code-gap">The model code gap</a> is an idea from George Fairbanks’s book “<a href="https://www.georgefairbanks.com/book">Just enough software architecture.</a>” It describes the conceptual gap between the abstractions we use to discuss software architecture (a model) and the reality of the executed source code. — From <a href="https://icepanel.io/blog/2022-11-30-the-model-code-gap">The model code gap | IcePanel Blog</a></blockquote><p>To remediate the model-code gap, we have several tools and techniques that we can use to “<a href="http://www.kamilgrzybek.com/design/modular-monolith-architecture-enforcement/">Enforce the architecture</a>” (I recommend reading the article as it goes into details about Architecture enforcement). These tools can be summarized from faster and limited, to slower and flexible, as follows:</p><p>1 —<strong> Compile time enforcement</strong>: This category contains everything that we have in the language that allows us to enforce encapsulation, packaging, and dependency rules and directions. (e.g.: private vs. public fields, package dependencies, etc.). <br>2 — <strong>Automated checks</strong>: This includes extra configuration or extra code that we use specifically to allow/forbid certain things to be done in our codebase. Static code analysis technique is part of this category and in some ecosystems, we can write tests for the architecture of the code by using tools like <a href="https://archunitnet.readthedocs.io/en/latest/">ArchUnitNET</a><br>3 — <strong>Code Review</strong>: This involves the human factor, either with pair programming or asynchronous code reviews in order to “manually” check that the architecture is respected and that the code adheres to the desired design that’s probably has been documented in an <a href="https://adr.github.io/">Architectural Decision Record</a>.</p><p>Theory aside, when it comes to the code, the architecture is not boxes, nor arrows. It’s which parts of the code are together (cohesion) and who is calling it (coupling). So, enforcing the architecture boils down to putting the code in the desired place and allowing it to be called from where we want it to be called. And this article tries to go through what we have in our hands as flutter developers in order to achieve that.</p><h3>The Flutter case:</h3><h4>Motivation :</h4><p>Looking at the Flutter ecosystem, most of us (I am a culprit as well) tend to just create a project and start right by putting everything in the lib/ directory, without caring too much about the boundaries of our app, even if we had a specific architecture in mind (like in <a href="https://github.com/ResoCoder/flutter-tdd-clean-architecture-course">here</a> and <a href="https://github.com/nanosoftonline/clean-flutter">here</a>), we tend to think of it in terms of folders and files which in reality doesn’t have any enforcement at all, and adding a dependency from a folder to another is just a matter of accessing the IDE actions and auto import the missing file which adds the famous <em>import ‘file.dart’</em> at the start of the file. <br>The ease of such an “import” mechanism is like having a tap that’s ready to be opened, at any time, for free, to add water to the big bull of mud we are forming.</p><h4>The toolbox :</h4><p>Flutter, or more concretely, dart, has many mechanisms that we can leverage in order to enforce our architecture and apply one of the above-mentioned techniques. We will also try to cover the pros/cons of each one, so we can choose the right one for our needs.</p><p>For the sake of demo and simplicity, I create a <a href="https://github.com/karimkod/architecture_enforcement_in_flutter">simple/sample app</a> to query a question from the <a href="https://the-trivia-api.com/">Trivia API</a>.</p><h4>1-The single file case :</h4><p>Since dart provides two access modifiers which are public and library private, then we can use that in order to control our architecture. The idea is to put all relevant parts of functionality in one file (which is by default one library) and only let the public what we need from our UI, this technique is <em>compile-time enforcement</em>.</p><p>You can find the example <a href="https://github.com/karimkod/architecture_enforcement_in_flutter/blob/main/single_file_case/lib/trivia_quiz.dart">here</a>. <br>In our case, we have a <em>QuizzService</em> which is a service that will contain different use cases, for now, it contains only one use case, “Get Random Question”. The only parts that we need public are the service and the <em>Quizz</em> Type. The other ones (like the repository interface and implementations) are all private.</p><p><strong>Pros : <br>1 — Allow strict control of the public API:</strong> only the service and the domain model are exposed.<strong><br>2 — Cohesion is very high: </strong>since we bundled everything related to the functionality in one file, we have everything together (but this is both a blessing and a curse)<br><strong>3 — Simple structure</strong>: using the files makes it easier to navigate the project and the folder structure is very flat. <br><strong>4— Fits a package (file, in this case) by feature design.</strong></p><p><strong>Cons : <br>1 — Maintainability/Readability:</strong> Having everything in one file may make it difficult to different code parts and may make the readability of the code suffer. Especially, if the file gets bigger and parts of it (like Database, or HTTP calls) get complicated or very low level. This can be solved by having multiple files with the “library/part/part of” features of dart that will allow us to create multiple files that are logically equivalent to the same file but these mechanisms are <a href="https://dart.dev/guides/libraries/create-library-packages#organizing-a-library-package">discouraged</a> by the dart team, and they are poorly documented anyway.<br>2 — <strong>One-way protection: </strong>Although we have protected the UI from using the repository directly we don’t have that protection the other way around. So, our single file can still import a Widget (or any other type) and reference it without any warning/error.<br>3<strong> — Rigid:<br><em>For testing</em></strong>: it is difficult to perform a simple unit test without having <a href="https://enterprisecraftsmanship.com/posts/code-pollution/">code pollution</a> ( _FakeRepository<em> and </em><em>testCompose()</em>) And that’s a byproduct of our hard boundary. We may push toward having dependency injection, but this will defy the purpose of having everything in one file since we will break the hard boundary we set with the privacy mechanism. But if we can afford to have only integration tests, then that’s maybe fine. I tried to use the ‘library/part/part of’ keywords, but their functionality doesn’t seem to span the test folder — I couldn’t say for sure, since I couldn’t find any official documentation. <br><strong><em>For extensibility and reusability</em></strong>: If we have code or logic we want to share across features then we are obliged to have everything in the same file or else create a duplication — the logic is not part of a public API —.</p><p>I personally don’t favor this solution too much, for the different cons I mentioned above and I felt like fighting it in order to test it. and as I do TDD, I sometimes need the option to gear down and test logic in entities especially if it’s a complex logic (or logic that can create a combinatorial explosion if tested from a client class/code, etc.) But this is my opinion and different people have different <a href="https://www.christianfindlay.com/blog/test-isolation-expensive">takes</a>, and there is always a trade-off between <a href="https://youtu.be/9imyW_WAYP4?t=2018">granularity and testability</a>.</p><h4>2 — Package and Analyze :</h4><p>This approach leverages the use of dart (or flutter) <a href="https://dart.dev/guides/libraries/create-library-packages">packages</a> and <a href="https://dart.dev/guides/language/analysis-options">dart static analyzer</a>. And as we will see shortly it can be either <strong>an automated check</strong> or a <strong>compile-time enforcement</strong>.</p><p><em>The difference between dart and flutter packages is that the latter depends on flutter SDK and the former only depends on dart SDK. I tend to use dart packages whenever I have packages that don’t need any UI elements or types and only keep dependency from Flutter on the UI (usually, the main project) package.</em></p><p>The idea of this approach is to create the flutter project and create several other packages to encapsulate what we want to package together, for example in the case of a clean architecture we can have a package by layer, like a core package, an infrastructure layer, etc. In the case of a vertical slice architecture, we can package by feature or package by a set of cohesive features. <br>In our example, I have created a package called trivia_quiz that contains the logic and all the types related to getting a random question from Trivia API. The package also contains the related unit and contract tests.</p><p>You can find the example <a href="https://github.com/karimkod/architecture_enforcement_in_flutter/tree/main/package_by_layer">here</a>.</p><p>Alongside the package organization, we can use a dart analyzer to enforce the import style and only allow import statements referencing the package and not single files inside the packages with relative file imports. As you can see in the analysis_options.yaml I have marked the two analyzer’s hints to be errors, this way I have a stronger alert when they happen. I also needed to enable always_use_package_importssince it’s not enabled in the imported flutter.yaml file. unlike the avoid_relative_lib_imports.</p><pre>include: package:flutter_lints/flutter.yaml<br><br>analyzer: <br>  errors: <br>    avoid_relative_lib_imports: error<br>    always_use_package_imports: error<br><br>linter:<br>  rules:<br>    - always_use_package_imports</pre><p>Now, if we have any dart-enabled IDE, or we run flutter analyze we can see the errors when we try to import something using a relative path, bypassing the public API of the package. (the <a href="https://dart.dev/guides/libraries/create-library-packages">documentation</a> explains very well how the packages work, so I recommend reading it).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/949/1*4DUa0Me_tILnrxFQiCdYPQ.png" /><figcaption>error when trying to relatively import a file inside the src folder of a package.</figcaption></figure><p>It’s also possible to integrate flutter analyze in your CI pipeline in order to gatekeep commits/PR from violating the aforementioned rules.</p><h4>Where to create the package :</h4><p>The behavior of this approach depends on where you put the packages you create: <br><strong>i — Inside the flutter’s lib folder:</strong> This will raise the linter errors mentioned earlier (the case of the previous screenshot) but doesn’t force you to import the package in flutter’s pubspec.yml. <br><strong>ii — Outside the flutter’s lib folder:</strong> This will raise the linter errors mentioned as well as a compilation error and the project won’t compile at all, so you have a much stronger prevention from importing things you don’t want to import. And any package you need to use needs to be referenced in the pubspec.yml of the referencing package. The error triggered is the following :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/943/1*iEOyy_Huh59U4wUIz8Vm0Q.png" /><figcaption>Compilation error when trying to access the file of a package relatively</figcaption></figure><p><strong>Pros : <br>1 — Allow control of the public API: </strong>We can have as many files as we want in the package but we can pick which ones we want other packages to depend on using the export and show mechanism. <strong><br>2 — Control package dependencies: </strong>Since each package has its own pubspec.yaml we can control what each package depends on explicitly, and figuring out what the package uses is pretty easy. <strong><br>3 — Easier to maintain and extend: </strong>Having a full package with the source code and the test together makes it nicer and easier to maintain. And we don’t worry much about what to put public and private (at least inside a package). <br><strong>4 — Natural:</strong> compared to the single file solution, it feels more natural to work with packages to achieve enforcement.</p><p><strong>Cons : <br>1 — Too many folders, too many files:</strong> The number of packages can create a complicated folder structure that can lead to difficulty to work on the project, especially when working on features that span multiple packages (but this depends on how we structure our packages). <strong><br>2 — Relying only on the linter errors:</strong> Linter errors are fake errors, they don’t prevent the app from compiling, so enforcing this will need a CI that runs the analyzer and gatekeep the commits that break the linter rules. But this depends on whether you choose i or ii options above.</p><p>I personally favor this over the single file case and It’s my current way of working once the project starts to get a bigger codebase and a team starts growing. (really, when needed).</p><h4><strong>3 — External packages:</strong></h4><p>The approaches we discussed so far are native to the language and the framework. Alongside that, We can add extra layers of enforcement using external packages:</p><p><strong>1 — </strong><a href="https://github.com/kawa1214/import-lint"><strong>import_lint</strong></a><strong>:</strong> this can be very practical in our use case since it provides lint rules to analyze imports in the codebase. From the README:</p><pre>analyzer:<br>    plugins:<br>        - import_lint<br><br>import_lint:<br>    rules:<br>        use_case_rule:<br>            target_file_path: &quot;use_case/*_use_case.dart&quot;<br>            not_allow_imports: [&quot;use_case/*_use_case.dart&quot;]<br>            exclude_imports: [&quot;use_case/base_use_case.dart&quot;]<br>        repository_rule:<br>            target_file_path: &quot;repository/*_repository.dart&quot;<br>            not_allow_imports:<br>                [<br>                    &quot;use_case/*_use_case.dart&quot;,<br>                    &quot;repository/*_repository.dart&quot;,<br>                    &quot;space\ test/*.dart&quot;,<br>                    &quot;repository/sub/**/*.dart&quot;,<br>                ]<br>            exclude_imports: []<br>        domain_rule:<br>            target_file_path: &quot;domain/**/*_entity.dart&quot;<br>            not_allow_imports: [&quot;domain/*_entity.dart&quot;]<br>            exclude_imports: [&quot;domain/base_entity.dart&quot;]<br>        package_rule:<br>            target_file_path: &quot;**/*.dart&quot;<br>            not_allow_imports: [&quot;package:import_lint/import_lint.dart&quot;]<br>            exclude_imports: []<br>        core_package_rule:<br>            target_file_path: &quot;package:core/**/*.dart&quot;<br>            not_allow_imports: [&quot;package:module/**/*.dart&quot;]<br>            exclude_imports: []<br></pre><p>This specifies which imports are not allowed not_allow_imports for the files that are included in the target path target_file_path.<br>This package inherits what we said about the usage of the analyzer earlier but it offers more fine-grained control over the dependencies inside our codebase. The package looks maintained and it has 85% in popularity. I personally would use it if I needed it.</p><p><strong>2 — </strong><a href="https://pub.dev/packages/dart_code_metrics"><strong>dart_code_metrics</strong></a><strong>:</strong> Although this is a general static analysis package, it provides a rule that may be of use to us, avoid-banned-imports and you can define the banned imports as follows :</p><pre>dart_code_metrics:<br>  rules:<br>    - avoid-banned-imports:<br>        entries:<br>        - paths: [&quot;some/folder/.*\.dart&quot;, &quot;another/folder/.*\.dart&quot;]<br>          deny: [&quot;package:flutter/material.dart&quot;]<br>          message: &quot;Do not import Flutter Material Design library, we should not depend on it!&quot;<br>        - paths: [&quot;core/.*\.dart&quot;]<br>          deny: [&quot;package:flutter_bloc/flutter_bloc.dart&quot;]<br>          message: &#39;State management should be not used inside &quot;core&quot; folder.&#39;</pre><p>This package is also very popular and maintained, and it has other usages than only import banning. This can be of use if you already use this package in your projects and don’t want to depend on another static analysis package.</p><p><strong>3 — </strong><a href="https://github.com/gustavo-hillesheim/dart_arch_test"><strong>dart_arch_test</strong></a><strong>:</strong> This package provides a testing toolset that allows you to build automated tests to define the dependency rules in your codebase. Like :</p><pre>import &#39;package:my_package/main.dart&#39;;<br>import &#39;package:arch_test/arch_test.dart&#39;;<br><br>void main() {<br>  archTest(classes.that<br>    .areInsideFolder(&#39;entity&#39;)<br>    .should<br>    .extendClass&lt;BaseEntity&gt;(),<br>  );<br>}</pre><p>Since this is an automated test, it can be integrated into the CI and it can be a strong indicator that we introduced a gap between our code and model. <br>Unlike the first package, this one doesn’t look popular and it only targets Linux, macOS, and Windows (from what I can see in the <a href="https://pub.dev/packages/arch_test">pub.dev</a>), and that’s really a bummer for a flutter developer. And, looking at the repo I can see that there is a work in progress in some branches but not much activity.</p><h3>Closing words :</h3><p>I wrote this article to explore different techniques that a flutter dev or a team can adopt in order to enforce an architecture in a codebase. And also I got frustrated about how easy it was to just depend on something with an import statement that probably the IDE added and not have any error or warning when we break a dependency rule (<a href="https://stackoverflow.com/questions/74288334/is-there-a-way-to-prevent-importing-files-from-flutter-framework-in-dart">there is at least another person like me</a>). <br>This may or may not be the case for everyone, some people prefer doing code reviews in order to keep the codebase in shape, but these are not an alternative to that but tools we can use alongside it.</p><p>If you have read this far, then thank you, and let me know if I missed something or if you have achieved architecture enforcement in a different ways. <br>You can follow me on <a href="https://twitter.com/karimkos">Twitter</a> and <a href="https://www.linkedin.com/in/abdelkrim-bournane/">LinkedIn</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5618823925b7" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/architecture-enforcement-in-flutter-apps-5618823925b7">Architecture Enforcement in Flutter Apps</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Open window loses focus issue on Windows, the reason is WSL.]]></title>
            <link>https://medium.com/@abdelkrim.bournane/open-window-loses-focus-issue-on-windows-the-reason-is-wsl-afade5e1b05e?source=rss-908f733155fd------2</link>
            <guid isPermaLink="false">https://medium.com/p/afade5e1b05e</guid>
            <category><![CDATA[wsl]]></category>
            <category><![CDATA[issues]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[docker-desktop]]></category>
            <category><![CDATA[windows]]></category>
            <dc:creator><![CDATA[Abdelkrim Bourennane]]></dc:creator>
            <pubDate>Wed, 16 Nov 2022 20:24:30 GMT</pubDate>
            <atom:updated>2022-11-17T02:15:20.204Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/758/1*e43YYm_XZI-inn_0-g4K3w.jpeg" /><figcaption><a href="https://ezpublish-france.fr/wp-content/uploads/2021/12/Windows-11-peut-desormais-installer-WSL-depuis-le-Microsoft-Store-758x379.jpg">Source</a></figcaption></figure><p>I had an issue that was very annoying and disturbing, and I didn’t notice it until I focused very well on it and had to diagnose it to find what’s the problem really is and find a solution (if we can call it that) to it.</p><h4>The symptoms :</h4><p>Any window you are using will lose focus and if you were typing into it, you will lose the ability to do so until you take the mouse and click on it in order to be able to type again. And this happens frequently until it becomes really annoying. (Sorry, I got mad).</p><h4>The analysis :</h4><p>After noticing the issue, I had to find a way to diagnose this and see why I was losing the window focus.</p><p>I had the idea to see what window has focus or what window/program is taking focus temporarily. So, I found this <a href="https://stackoverflow.com/a/70010344/8929299">PowerShell </a>script that prints the windows that currently have a focus in the console, and the good thing is that it runs continuously (while(1), who said infinite loops are bad).</p><pre>Add-Type  @&quot;<br> using System;<br> using System.Runtime.InteropServices;<br> using System.Text;<br>public class APIFuncs<br>   {<br>    [DllImport(&quot;user32.dll&quot;, CharSet = CharSet.Auto, SetLastError = true)]<br>   public static extern int GetWindowText(IntPtr hwnd,StringBuilder<br>lpString, int cch);<br>    [DllImport(&quot;user32.dll&quot;, SetLastError=true, CharSet=CharSet.Auto)]<br>   public static extern IntPtr GetForegroundWindow();<br>    [DllImport(&quot;user32.dll&quot;, SetLastError=true, CharSet=CharSet.Auto)]<br>       public static extern Int32 GetWindowThreadProcessId(IntPtr hWnd,out<br>Int32 lpdwProcessId);<br>    [DllImport(&quot;user32.dll&quot;, SetLastError=true, CharSet=CharSet.Auto)]<br>       public static extern Int32 GetWindowTextLength(IntPtr hWnd);<br>    }<br>&quot;@<br> <br>while(1)<br>{<br>$w = [apifuncs]::GetForegroundWindow()<br>$len = [apifuncs]::GetWindowTextLength($w)<br>$sb = New-Object text.stringbuilder -ArgumentList ($len + 1)<br>$rtnlen = [apifuncs]::GetWindowText($w,$sb,$sb.Capacity)<br>write-host &quot;Window Title: $($sb.tostring())&quot;<br>sleep 1<br>}</pre><p>The result was the following :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/510/1*kAwWQ0JgpgYHLRbRAZ36zQ.png" /></figure><p>As you can see, this “RemoteApp” is taking over the focus from my “Google Chrome” Browser.</p><p>And after a couple of Google searches, I found out that this app is the Windows Remote Desktop Connection (RDP) app.</p><p>So, I opened the process explorer (<a href="https://learn.microsoft.com/en-us/sysinternals/downloads/process-explorer">you can find it and install it here</a>) This is a verbose and detailed alternative for Windows Task Manager. I filtered by the name of the RDP process, which is (mstsc.exe). And found this :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/910/1*S505ppO_Y4g7o2YF0Lmd8w.png" /></figure><p>I was worried because I thought maybe someone has access to my computer over RDP. So, I check the <a href="https://www.anyviewer.com/how-to/view-connection-history-remote-desktop-windows-10-2578.html">history of RDP connections in my PC</a> and it was empty, then I checked if <a href="https://www.anyviewer.com/how-to/how-to-check-if-remote-desktop-is-enabled-0007.html">RDP was even enabled on my PC</a> but found nothing as well.</p><p>So, I figured that if you double-click on the process in Process Explorer you can see what command line launched the process, and here was the Eureka moment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/499/1*AIsDrLcne7G9YhjYyqen5Q.png" /></figure><p>It was WSL (Windows Subsytem of linux, which I personally need to dev) and I have various tools that need WSL, like Docker.</p><p>I didn’t do any further research on why WSL needs an RDP connection, but it was stealing my focus (literally). Found other people complaining about the same problem. (like <a href="https://youtrack.jetbrains.com/issue/IDEA-282196/Constant-loss-of-editor-focus-when-using-Windows-11-and-WSL">here</a>, the last comment states that it has nothing to do with any JetBrains tools which I confirm, it’s not a JetBrains issue, I use Rider and IntelliJ.)</p><h3>The solution</h3><p>I couldn’t find anything useful or a reasonable fix apart from restarting WSL using a command line to shut it down (be careful, if you have apps using WSL this will kill them or make them misbehave):</p><pre>wsl --shutdown</pre><p>you can run the following command to make sure all WSL are shut down :</p><pre>wsl --list --verbose</pre><p>And then run the following to restart it (or restart any app that uses it, it will launch it automatically) :</p><pre>wsl<br></pre><p>Thanks for reading this far, this was a quick post that maybe will help someone in the future. If you know a fix that I couldn’t find or have an insight about this issue, please put it in a comment or reach out to me, I can add it to this post with credits (of course!).</p><p>You can follow me on <a href="https://twitter.com/karimkos">Twitter </a>or <a href="https://www.linkedin.com/in/abdelkrim-bournane/">LinkedIn </a>for Software engineering topics and discussions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=afade5e1b05e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Toolbox for porting your Flutter Mobile Application to the Web]]></title>
            <link>https://medium.com/@abdelkrim.bournane/guide-to-porting-your-flutter-mobile-application-to-web-da1ce4f89301?source=rss-908f733155fd------2</link>
            <guid isPermaLink="false">https://medium.com/p/da1ce4f89301</guid>
            <category><![CDATA[flutter-app-development]]></category>
            <category><![CDATA[google]]></category>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Abdelkrim Bourennane]]></dc:creator>
            <pubDate>Mon, 18 Oct 2021 13:40:18 GMT</pubDate>
            <atom:updated>2021-10-18T14:06:29.788Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gnHqSRM2dV50E7xAoe1Lew.png" /></figure><p>Since Google announced that <a href="https://flutter.dev/web">Flutter web</a> is stable starting from v2.0.0, a lot of business and application owners wanted to have a version of their already developed Flutter application ported to the web, since the web is easily accessible and Progressive Web Applications (PWA) offer an experience close to native apps.<strong><em> This article doesn’t discuss UI matters (responsiveness, UI/UX…), it only tackles the problems we may encounter for running the app and its functionalities on the web platform.</em></strong></p><p>Since that announcement, I’ve been on several projects of porting Flutter mobile applications to the web. And I have organized a set of steps and technics that I follow every time I have a Flutter application to port to the web. These steps make it easier for me to detect what is not supported on the web and, depending on the situation, fix it or change it.</p><h4>1 — Application analysis :</h4><p>The first step is analyzing the application, the goal of this step is to check if there are any dependencies that are not supported on the web. The first thing to do is try to run for the web by executing</p><pre>flutter run -d chrome</pre><p>Or change the target to chrome and run it from your IDE.</p><p>This step is just to discover if there is anything that’s preventing the compilation to the web <em>(probably the use of a dart library that’s not supported on the web. If any, refer to 2 —</em>The toolbox<em> part of this article)</em> <strong>If the application ran properly it doesn’t mean that it’s working on the web and our job is done!</strong> Some issues may appear at <em>runtime</em>.</p><p>Now, if the application ran without any compilation error we proceed with the second phase of the application analysis, which is analyzing the dependencies. There are two kinds of dependencies that may interfere with our goal :</p><p><strong>I —Dart libraries :</strong><br> Sometimes developers use dart <a href="https://dart.dev/guides/libraries">core libraries</a> for some specific functionalities like using the File type (dart:io) and JSON encoding/decoding (dart:convert). Well, some of these core libraries are only supported in dart native platforms and not on the web and vice-versa. So, part of our job is to check if we are referring to any of these in our code. You can check the compatibility of the library with the target platform on the <a href="https://dart.dev/guides/libraries">official page</a>.</p><p><strong>II — external packages : </strong><br>The other resort for developers to find a tool that fits their needs is the famous <a href="https://pub.dev/">pub.dev</a>, we refer to the package and the version we want to use in the <a href="https://dart.dev/tools/pub/pubspec">pubspec.yaml</a> file. So, our job is to go through that list (manually for now) and check if the package is web-compatible. We can know by checking the pub.dev page of that package and check the platform tags of the version we depend on. For instance, the 7.3.1 version of <a href="https://pub.dev/packages/flutter_bloc">flutter_bloc</a> package is supported in Android, IOS, Linux, macOS, Web, and Windows and we can see that in the tags (we can change the version of the package in the versions tab):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/903/1*Id_cNW4dyMdD390unB2TOw.png" /></figure><p>After we have identified the blockers we start tackling them one by one using the appropriate tool each time.</p><h4>2 — The toolbox :</h4><p>In this part, we will go through the tools that will help us tackle the blockers we identified. The usage of each tool depends on your situation. The goal of this enumeration is to put the tools we have in front of us.</p><p><strong>I — Alternatives : </strong><br> The first smart step we can do is finding alternatives. This can either be a recent version of the same package or a completely different package. Most packages’ authors have upgraded their packages to support the web, it’s a good reflex to go check if the packages have a recent version that supports web or, sometimes, it refers to another package that’s considered a fork/upgrade of it that does. <br>Another factor that comes into play when replacing a package (especially if we didn’t make a <em>boundary </em>between it and our code) is the time and effort of the substitution.</p><p>Sometimes, we can’t find that alternative or it’s costly to adopt it. Then, It’s practical to keep the mobile code as it is and implement something web-specific.</p><p><strong>II — platform-specific code :</strong><br> Flutter and dart offer many ways to write and selectively execute platform-specific code.</p><p>To select which platform code to execute, dart has two mechanisms that help us decide in case of the web:</p><p><strong>a) <em>kIsWeb</em> flag: <br></strong> The <a href="https://api.flutter.dev/flutter/foundation/kIsWeb-constant.html"><strong>kIsWeb</strong></a><strong> </strong>is a boolean that will be set to true if your application is running on a web platform. This boolean is pretty useful to execute code that’s specific to the web.</p><p><strong>b) <em>Conditional imports</em>:<br></strong>Sometimes, we used libraries that prevent our code from even compiling to a specific platform (case of dart platform libraries — see above). The solution to that is to <strong>conditionally import a library</strong>. Conditional imports (it’s polymorphism for packages) let you import a package instead of another one if a condition is met, though, they need to provide the same interfaces (just like polymorphism) so that the substitution keeps the code valid. <br>If, for example, we have package abc that is supported on mobile and xyz that’s supported on the web and they provide the same interfaces. We can write :</p><pre>import &#39;package:abc/abc.dart&#39; <br>       if(dart.library.js) &#39;package:xyz/xyz.dart&#39; // in case of web</pre><p>I can’t go through all the details about it (for the article-length matter) but it’s useful to have that tool in our toolbox, here is an <a href="https://medium.com/flutter-community/conditional-imports-across-flutter-and-web-4b88885a886e">article</a> that explains it in detail alongside a use case.</p><p>Now, having mechanisms to help us select what platform code to execute, we can use the tools that help us write web-specific code.</p><p><strong>a) <em>Using a javascript code/library :</em> <br></strong>As you may know, dart supports Javascript interoperability which allows it to deal with JS code. We can use this to our advantage. Sometimes we find out that the official flutter SDK we are using (for my case it was braintree payment SDK by the time of the writing) doesn’t support flutter web ‘officially’. And that solution has an official js library. We can, using js interoperability; use that js library on the flutter web.<br>The <a href="https://pub.dev/packages/js">js package</a> can help us do that. It is used by defining dart classes/functions that are equivalent to the JS code and let their implementation be provided from javascript (by using the external keyword) And then we mark the dart code with annotations. For example, we can use JSON.stringify from dart by, first, defining the equivalent dart code:</p><pre><a href="http://twitter.com/JS">@JS</a>()<br>library stringify;</pre><pre>import &#39;package:js/js.dart&#39;;</pre><pre><a href="http://twitter.com/JS">@JS</a>(&#39;JSON.stringify&#39;)<br>external String stringify(Object obj);<br></pre><p>and then just call the stringify function in a dart code. For an advanced example, refer to the <a href="https://github.com/google/chartjs.dart/">charjs.dart repo</a> (I often use it as my reference).</p><p><strong>V — Extending packages :</strong><br>Now, if you still want to use one flutter package for all platforms (for any reason you have) or you think that changing to another package can be costly. You can still use the open-heart surgery option (thanks to the open-source nature of flutter and flutter packages). This solution may seem complicated but once we know the basics of creating flutter packages and read some useful articles about it (with good repository references and examples) it becomes more accessible to do it (depending on our knowledge of the target platform) All we have to do is fork the package and start working on it. <br>Unfortunately, I can’t go through all the details about extending packages but the following list contains the resources (articles and git repositories) I used to get the job done :</p><ul><li><a href="https://flutter.dev/docs/development/packages-and-plugins/developing-packages#plugin">Official documentation of writing packages</a>.</li><li><a href="https://medium.com/u/ab0973d194a3">Harry Terkelsen</a>’s articles about adding web support for url_launcher package: using Method Channels <a href="https://medium.com/flutter/how-to-write-a-flutter-web-plugin-5e26c689ea1">Part 1</a> OR using Federated plugin <a href="https://medium.com/flutter/how-to-write-a-flutter-web-plugin-part-2-afdddb69ece6">Part 2</a>.</li><li><a href="https://github.com/flutter/plugins/tree/master/packages/url_launcher/url_launcher">url_launcher repository</a> is a good reference.</li></ul><p>I have personally used this to extend the <a href="https://github.com/Baseflow/flutter-geocoding/issues/69#issuecomment-924418631">geocoding</a> package, which was a federated plugin; to support the web.</p><h4>Conclusion</h4><p>Now, we did what’s necessary but the only way to know if our job is done is by going through an efficient test process (favorably, automatic, or else manual). <br>Having this toolbox in hand can, hopefully; be useful to you. If you have any feedback/comments/Critics or you just wanna connect. please reach out to me through my <a href="https://www.linkedin.com/in/abdelkrim-bournane/">LinkedIn</a> or leave them in the comment section.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=da1ce4f89301" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>