<?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 Guillaume Roux on Medium]]></title>
        <description><![CDATA[Stories by Guillaume Roux on Medium]]></description>
        <link>https://medium.com/@rouxguillaume?source=rss-7e7bfc0e26d4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*zbqqJwpU-JcBfmDr.jpg</url>
            <title>Stories by Guillaume Roux on Medium</title>
            <link>https://medium.com/@rouxguillaume?source=rss-7e7bfc0e26d4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 05:59:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rouxguillaume/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[TDD in Flutter Part 4: Creating Mocks]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://itnext.io/tdd-in-flutter-part-4-creating-mocks-eb3b0a38bae0?source=rss-7e7bfc0e26d4------2"><img src="https://cdn-images-1.medium.com/max/2600/1*cnOxe4bps8i07G-atX2c0w.jpeg" width="6251"></a></p><p class="medium-feed-snippet">Welcome to this 4th episode of the &#x201C;Test Driven Development in Flutter&#x201D; series, today I&#x2019;m going to explain the concept of &#x201C;Mocking&#x201D;&#x2026;</p><p class="medium-feed-link"><a href="https://itnext.io/tdd-in-flutter-part-4-creating-mocks-eb3b0a38bae0?source=rss-7e7bfc0e26d4------2">Continue reading on ITNEXT »</a></p></div>]]></description>
            <link>https://itnext.io/tdd-in-flutter-part-4-creating-mocks-eb3b0a38bae0?source=rss-7e7bfc0e26d4------2</link>
            <guid isPermaLink="false">https://medium.com/p/eb3b0a38bae0</guid>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[flutter-app-development]]></category>
            <category><![CDATA[test-driven-development]]></category>
            <dc:creator><![CDATA[Guillaume Roux]]></dc:creator>
            <pubDate>Wed, 24 Aug 2022 11:56:48 GMT</pubDate>
            <atom:updated>2022-08-24T15:43:03.670Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[What’s in my VSCode? — For Flutter development]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://itnext.io/whats-in-my-vscode-for-flutter-development-48ee88e0adaf?source=rss-7e7bfc0e26d4------2"><img src="https://cdn-images-1.medium.com/max/1280/1*bz6eXbVdL7suSiTL3sZ4dA.png" width="1280"></a></p><p class="medium-feed-snippet">My main editor, when it comes to writing code, has been for several years Visual Studio Code. It&#x2019;s free, open-source and has a good plugin&#x2026;</p><p class="medium-feed-link"><a href="https://itnext.io/whats-in-my-vscode-for-flutter-development-48ee88e0adaf?source=rss-7e7bfc0e26d4------2">Continue reading on ITNEXT »</a></p></div>]]></description>
            <link>https://itnext.io/whats-in-my-vscode-for-flutter-development-48ee88e0adaf?source=rss-7e7bfc0e26d4------2</link>
            <guid isPermaLink="false">https://medium.com/p/48ee88e0adaf</guid>
            <category><![CDATA[flutter-app-development]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[vscode]]></category>
            <category><![CDATA[visual-studio-code]]></category>
            <category><![CDATA[dart]]></category>
            <dc:creator><![CDATA[Guillaume Roux]]></dc:creator>
            <pubDate>Fri, 05 Aug 2022 10:16:30 GMT</pubDate>
            <atom:updated>2022-08-05T10:38:13.873Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[TDD in Flutter - Special: Golden tests with Alchemist ]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://itnext.io/tdd-in-flutter-special-golden-tests-with-alchemist-ea8c96ff4dfe?source=rss-7e7bfc0e26d4------2"><img src="https://cdn-images-1.medium.com/max/2600/1*cnOxe4bps8i07G-atX2c0w.jpeg" width="6251"></a></p><p class="medium-feed-snippet">Learn the basics of the alchemist package to write golden tests in Flutter.</p><p class="medium-feed-link"><a href="https://itnext.io/tdd-in-flutter-special-golden-tests-with-alchemist-ea8c96ff4dfe?source=rss-7e7bfc0e26d4------2">Continue reading on ITNEXT »</a></p></div>]]></description>
            <link>https://itnext.io/tdd-in-flutter-special-golden-tests-with-alchemist-ea8c96ff4dfe?source=rss-7e7bfc0e26d4------2</link>
            <guid isPermaLink="false">https://medium.com/p/ea8c96ff4dfe</guid>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[flutter-app-development]]></category>
            <category><![CDATA[test-driven-development]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[flutter]]></category>
            <dc:creator><![CDATA[Guillaume Roux]]></dc:creator>
            <pubDate>Mon, 04 Apr 2022 14:49:06 GMT</pubDate>
            <atom:updated>2022-07-19T07:41:20.741Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[TDD in Flutter Part 3: Testing your widgets]]></title>
            <link>https://itnext.io/tdd-in-flutter-part-3-testing-your-widgets-c5e87d76a864?source=rss-7e7bfc0e26d4------2</link>
            <guid isPermaLink="false">https://medium.com/p/c5e87d76a864</guid>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[flutter-app-development]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[test-driven-development]]></category>
            <category><![CDATA[dart]]></category>
            <dc:creator><![CDATA[Guillaume Roux]]></dc:creator>
            <pubDate>Mon, 20 Dec 2021 21:50:14 GMT</pubDate>
            <atom:updated>2022-02-23T20:44:25.675Z</atom:updated>
            <content:encoded><![CDATA[<p>Hi everyone, it has been a while since the last episode but finally here it is, and I am ready to explain to you how to test your widgets. In this 3rd part I will explain you how to use testWidgets and how to use snapshot comparison for your widget rendering with the package <a href="https://pub.dev/packages/golden_toolkit">golden_toolkit</a>.</p><p>And you can now find all previous articles in a dedicated <a href="https://rouxguillaume.medium.com/list/tdd-in-flutter-64d0d5f07854">List: TDD in Flutter</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cnOxe4bps8i07G-atX2c0w.jpeg" /></figure><h3>testWidgets()</h3><p>This method works the same way as the test() method and will basically execute your flutter code in a test environment. The main difference being that this test environment is made to build and interact with your widgets. To do so here’s some method you can use to simulate actions on your widgets through the WidgetTester:</p><ul><li>pumpWidget : It will simulate a call to runApp() with the given widget.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3dc071af30a217ae39e11dd4df01ded2/href">https://medium.com/media/3dc071af30a217ae39e11dd4df01ded2/href</a></iframe><ul><li>drag and dragFrom: Simulate a drag action on the given widget by the given offset.</li><li>ensureVisible : Given a widget W specified in a Finder and a Scrollable widget S in its ancestry tree, it will scroll S to make W visible.</li><li>enterText : Give the text input widget specified in the Finder the focus and replace its content with a given text as if it has been provided by the onscreen keyboard.</li><li>fling and flingFrom : Attempts a fling gesture starting from the center of a given widget, moving the given distance and reaching the given speed.</li><li>idle : Acts as if the application went idle.</li><li>press , longPress, longPressAt , tap and tapAt: Dispatch a pointer down/pointer up at the center of the given widget (assuming it is exposed) or location.</li><li>showKeyboard : Give the text input widget specified in the Finder the focus as if the onscreen keyboard had appeared.</li><li>startGesture : Creates a gesture with an initial down gesture at a particular point.</li></ul><p>As you might’ve understand with those methods, you might need to use a Finder which is a class that search a widget. You can use it to find a widget which you want to apply a specific action on or to ensure that a widget is correctly displayed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fc444dd7d81836a513122d041aa737fb/href">https://medium.com/media/fc444dd7d81836a513122d041aa737fb/href</a></iframe><h3>testGoldens()</h3><p>This test method is provided by the package <a href="https://pub.dev/packages/golden_toolkit">golden_toolkit</a>, it allows you to test your UI to ensure that it stays the same. This kind of test works by taking a snapshot image of your last UI and compares it with the new one. A golden test will be considered as failing if the new UI looks different than before.</p><p>Let’s take an example, here’s a simple widget that displays some text:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c8bf9641571740001518bd843112e5f3/href">https://medium.com/media/c8bf9641571740001518bd843112e5f3/href</a></iframe><p>This will display the content inside a Text widget with a predefined styling.</p><p>Now here’s our test file for the golden test:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce0b6e51bcbe8b32f32f0c7f200f35e6/href">https://medium.com/media/ce0b6e51bcbe8b32f32f0c7f200f35e6/href</a></iframe><p>The first thing to do before running your golden tests is to generate the initial snapshot that will be your matching sample. To do so, just use the command flutter test --update-goldens it will run all your unit tests but will only generate or update the snapshot for your golden tests. Here’s the output for our little test:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RQqynE5238i0pZobzkOiww.png" /><figcaption>test/goldens/simple_red_text.png</figcaption></figure><p>Now let’s suppose that I want to make my SimpleText widget more flexible in my application, by making the color variable for example. The code could evolve like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/687b23bd6a471f556dd382754ca513bf/href">https://medium.com/media/687b23bd6a471f556dd382754ca513bf/href</a></iframe><p>By following TDD principles, because our code is still compiling we don’t need to update our test. So let’s run it but this time without the — update-goldens parameter.</p><pre>$&gt; flutter test<br>00:01 +0: loading D:\Documents\Projects\so_tests\test\golden_test.dart</pre><pre>00:02 +0: golden: simple red text<br>══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════<br>The following assertion was thrown while running async test code:<br>Golden &quot;goldens/simple_red_text.png&quot;: Pixel test failed, 0.16% diff detected.<br>Failure feedback can be found at /D:/Documents/Projects/so_tests/test/failures</pre><pre>When the exception was thrown, this was the stack:<br>#0      LocalFileComparator.compare (package:flutter_test/src/_goldens_io.dart:100:7)<br>&lt;asynchronous suspension&gt;<br>&lt;asynchronous suspension&gt;<br>(elided one frame from package:stack_trace)<br>════════════════════════════════════════════════════════════════════════════════════════════════════<br>00:02 +0 -1: golden: simple red text [E]<br>  Test failed. See exception logs above.<br>  The test description was: golden: simple red text</pre><pre>00:02 +0 -1: Some tests failed.</pre><p>As you can see our test failed and we can check the output inside the test/failures folder:</p><ul><li>Here’s the expected image:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RQqynE5238i0pZobzkOiww.png" /><figcaption>simple_red_text_masterImage.png</figcaption></figure><ul><li>And the output generated during the test:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SW-FeC8BcI98oxWDKfsLyA.png" /><figcaption>simple_red_text_testImage.png</figcaption></figure><p>And you even get snapshot showing the differences between the two:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kNjO9xDKD7ECbm_MiwhaCQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WGrBwf1rTjektug7_WEp0A.png" /><figcaption>simple_red_text_isolatedDiff.png (left) — simple_red_test_maskedDiff.png (right)</figcaption></figure><p>So now that we have a non passing test we can update the code so it will be passing again:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d2b72073d185270c8b7f185e7c8cb1e9/href">https://medium.com/media/d2b72073d185270c8b7f185e7c8cb1e9/href</a></iframe><p>And if we run our test once again:</p><pre>$&gt; flutter test<br>00:03 +0: loading D:\Documents\Projects\so_tests\test\golden_test.dart</pre><pre>00:04 +1: All tests passed!</pre><p>Yay ! It’s passing again, but we haven’t tested yet if our new color parameter can be used for other color than the red so let’s add another test.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7e69a567325378562c451d6b6c90b4ed/href">https://medium.com/media/7e69a567325378562c451d6b6c90b4ed/href</a></iframe><p>And after re-generating our golden files here’s the new output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5WghEDul5e4D8WiUKsHJjg.png" /><figcaption>simple_blue_text.png</figcaption></figure><p>By doing those tests I’ve ensured that adding new capabilities to my SimpleText widget didn’t cause any regression on my UI, which is as important as unit-testing your code.</p><p>Another important piece of information, currently the golden files generated are not consistent between OS and Flutter versions. Which means that snapshots might be different if they are generated on Windows and MacOS or with a different Flutter version. The only solution for now seems to be to run your golden tests inside some container which will maintain a constant environment.</p><h3>Conclusion</h3><p>And here it is folks ! As always I hope that you liked this little article of mine. Once again, I am deeply sorry for the delay between the previous release. I had some other project to take care of. Nonetheless, if you still want to show your support for this series you can clap and or leave a comment, it is always a pleasure to read your messages. The next article will be on how to use the package <a href="https://pub.dev/packages/mockito">mockito</a> to mock your classes, so do not hesitate to follow my account if you don’t want to miss it. And if you want to check the previous articles, you can find them all in their dedicated reading list: <a href="https://rouxguillaume.medium.com/list/tdd-in-flutter-64d0d5f07854">https://rouxguillaume.medium.com/list/tdd-in-flutter-64d0d5f07854</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c5e87d76a864" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/tdd-in-flutter-part-3-testing-your-widgets-c5e87d76a864">TDD in Flutter Part 3: Testing your widgets</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[TDD in Flutter Part 2: How to write a unit test ?]]></title>
            <link>https://itnext.io/tdd-in-flutter-part-2-how-to-write-a-unit-test-8493044792f?source=rss-7e7bfc0e26d4------2</link>
            <guid isPermaLink="false">https://medium.com/p/8493044792f</guid>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[test-driven-development]]></category>
            <category><![CDATA[flutter-app-development]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[flutter]]></category>
            <dc:creator><![CDATA[Guillaume Roux]]></dc:creator>
            <pubDate>Mon, 12 Jul 2021 17:27:34 GMT</pubDate>
            <atom:updated>2022-02-23T20:43:49.659Z</atom:updated>
            <content:encoded><![CDATA[<h3>TDD in Flutter Part 2: How to write a unit test ?</h3><p>This article is a continuation of the first part <a href="https://itnext.io/tdd-in-flutter-part-1-introduction-to-test-driven-development-c130b9e82f36"><strong><em>TDD in Flutter Part 1: Introduction to Test Driven Development</em></strong></a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cnOxe4bps8i07G-atX2c0w.jpeg" /></figure><h3>Write your first unit test</h3><p>Let’s consider that you have created an application which fetch data from an API. How can you test that you will be fetching and parsing your data correctly into a model class ?</p><p>In this example I am going to use the <a href="https://jsonplaceholder.typicode.com/">JSON Placeholder API</a>. First thing you should do would be to grab some response which you could use for your tests, here’s mine:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b96a25e1f95a801b08f65dd29bb00a0c/href">https://medium.com/media/b96a25e1f95a801b08f65dd29bb00a0c/href</a></iframe><p>Next step would be to create some UserModel class, but remember that we are trying to apply TDD so the first thing you should do is to create a test file inside the test/ folder which will be the base to define our class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/486961a3964fefc630366e29df751e5a/href">https://medium.com/media/486961a3964fefc630366e29df751e5a/href</a></iframe><p>Note that I’ve called the file user_test.dart the “_test” part of the filename is important. It is needed so your file will be recognized as a test file.</p><p>What we did in our file is only defining that we need a UserModel class which will takes all four of our parameters defined in the JSON and will store their values so we can access them. Now that we have a failing test, we can create our UserClass with all the needed properties in our code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/12335700d311cb4e4f0497873970ca53/href">https://medium.com/media/12335700d311cb4e4f0497873970ca53/href</a></iframe><p>We can now try to run our test by using the command flutter test you should get an output similar to this:</p><pre>00:00 +0: test 1<br>00:00 +1: All tests passed!</pre><p>Because we have a passing test we need to write another or change our current one before coding again. Now that we have defined our model the next step would be to parse a sample JSON file so we can confirm that our UserModel would be usable to parse the API data. To use a file in your tests you can simply create a new folder inside test/ and add inside it all your test resources.</p><pre>- test/<br>  - test_resources/<br>    - random_user.json<br>  - user_test.dart</pre><p>And you can grab it from your code like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56878c0d1078c2845c86e4482914d878/href">https://medium.com/media/56878c0d1078c2845c86e4482914d878/href</a></iframe><p>We now have all we need to add a new test:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/90af8cf52fcaf4c3db9985eb2245996d/href">https://medium.com/media/90af8cf52fcaf4c3db9985eb2245996d/href</a></iframe><p>As you can see we have defined a new constructor UserModel.fromJson which we will need to code like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b9322fe5048b1dcb6ce40b34c1bf7fcf/href">https://medium.com/media/b9322fe5048b1dcb6ce40b34c1bf7fcf/href</a></iframe><p>And you are done ! Your class is fully tested, the last and final step would be to mock an HTTP client.</p><h3>Mocking an HTTP client</h3><p>For this article I am going to use the package <a href="https://pub.dev/packages/http">http</a> as it is the easiest to mock and test. First thing you should do is define the base class and method which will be used for your API call.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1cdbf6302c8e027dc763891798433a4e/href">https://medium.com/media/1cdbf6302c8e027dc763891798433a4e/href</a></iframe><p>What we want to do is simply use the MockClient provided by the package to simulate fake API calls, instead of receiving a response from the API we will return our sample file random_user.json.</p><p>Now we need to code our ApiProvider class and its method getUser() :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c44a1b61d5ee429b0971429fa0b66027/href">https://medium.com/media/c44a1b61d5ee429b0971429fa0b66027/href</a></iframe><p>As you can see our ApiProvider is taking an HTTP client as parameter, by doing so you are able to pass either the MockClient or a real Client object to perform your request. You should now be able to test your API calls.</p><h3>Conclusion</h3><p>Congratulations, this is the end of this 2nd part. As always I hope that I was clear in my explanations and if you liked this article and want to support this series you can clap it and leave your impressions in the comment section.</p><p>I want to thank the community for all the support you have shown for the first article, it is really motivating. The 3rd part will be dedicated to widget testing using testWidgets and snapshot comparison using the golden_toolkit .</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8493044792f" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/tdd-in-flutter-part-2-how-to-write-a-unit-test-8493044792f">TDD in Flutter Part 2: How to write a unit test ?</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[TDD in Flutter Part 1: Introduction to Test Driven Development]]></title>
            <link>https://itnext.io/tdd-in-flutter-part-1-introduction-to-test-driven-development-c130b9e82f36?source=rss-7e7bfc0e26d4------2</link>
            <guid isPermaLink="false">https://medium.com/p/c130b9e82f36</guid>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[test-driven-development]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[flutter-app-development]]></category>
            <dc:creator><![CDATA[Guillaume Roux]]></dc:creator>
            <pubDate>Sun, 04 Jul 2021 19:30:48 GMT</pubDate>
            <atom:updated>2022-02-23T20:43:19.256Z</atom:updated>
            <content:encoded><![CDATA[<p>In this series I am going to explain how you can apply principles of the Test Driven Development (TDD for short) to a Flutter (or Dart) project.</p><p>This first part will be dedicated to the basics of TDD and trying to understand when it is relevant to use it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cnOxe4bps8i07G-atX2c0w.jpeg" /></figure><h3>What is TDD ?</h3><p>You know what they say, a picture is worth a thousand words so here is a representation of the Test Driven Development cycle :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/489/1*KP_vX21WC5lig_PnHYomSw.png" /></figure><p>But what does it means concretely ? Well if we consider a newly created project before even starting coding anything you should be able to write a test that fails and then write the code needed for the test to pass, then you can refactor the code if needed and repeat the cycle again by writing another test.</p><p>Of course it is not always relevant to test everything in your application especially while developing in Flutter, you will rarelly need to test your entire UI and ensure that each of your AppBar are correctly displayed for example. But it might be interesting to unit test some API calls if your application is consuming data from an external service or make database related test if your app relies a lot on it. In the end TDD will hugely improve the stability and quality of your code especially if you are maintaining or contributing some open-source code. By doing so you will accomplish two things:</p><ol><li>You will be avoiding regression in your code or code writen by somebody else. If you decide to make some refacto to your code, add a behavior to it, or some service you were relying on changes, you will be able to immediatly see which part of the project doesn’t work as before.</li><li>It will be easier for your code to respect the<strong> Interface segregation principle</strong> and the <strong>Dependency inversion principle</strong> described in the <a href="https://en.wikipedia.org/wiki/SOLID">SOLID architecture</a>. Because to make your code testable you will need to separate your interface and abstract part of your code. (Thanks “Uncle Bob”)</li></ol><h3>The first test you will see in Flutter</h3><p>You might already know it but when you create a new Flutter project there is a test file generated in the test/ folder of your project for the base counter application.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ca6b1be281a63dcdfe104d6f677167d/href">https://medium.com/media/8ca6b1be281a63dcdfe104d6f677167d/href</a></iframe><p>The code is clear enough by itself, basically it will simulate your widget rendering and actions, its purpose is to simulate some user input and it will expect to contains some specific text widget.</p><p>It is called widget testing, the goal is to verify if a widget has the expected behavior. While it is also important to test your UI it is not always the easiest kind of test to write. In a future article we’ll see how to test your UI by using widgets snapshots comparison with the <a href="https://pub.dev/packages/golden_toolkit">golden_toolkit</a> package.</p><p>In Flutter you will find three categories of automated test:</p><ol><li><strong>Unit test</strong>: tests a single function, method or class.</li><li><strong>Widget test</strong>: tests a single widget.</li><li><strong>Integration test</strong>: tests a complete app or large part of an app.</li></ol><p>For now you might want to give a try to the unit tests, in my opinion they might be the easiest to write at the beginning. If you don’t know where to start don’t worry we will see how to write a unit test in the second part of this series.</p><p>If you cannot wait the next article you can already check the <a href="https://flutter.dev/docs/cookbook/testing/unit/introduction">introduction to unit testing</a> from the official Flutter documentation.</p><h3>Conclusion</h3><p>Thank you for reading this article I tried to stay simple in my explanations. In the next part I’ll explain how you can write a unit test, while I do not have a full roadmap I am planning to explain in the future how you can mock your dependencies and test your widget with snapshots comparison. If there is some specific subjects you would like to see in the next releases you can leave a comment.</p><p>And of course if you liked this article and want to show your support for this series you can clap it (up to 10 times) and/or leave a comment. Also if you are interested in the next part do not hesitate to follow me so you will be notified.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c130b9e82f36" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/tdd-in-flutter-part-1-introduction-to-test-driven-development-c130b9e82f36">TDD in Flutter Part 1: Introduction to Test Driven Development</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[Flutter Web: OAuth authentication through external window]]></title>
            <link>https://itnext.io/flutter-web-oauth-authentication-through-external-window-d890a7ff6463?source=rss-7e7bfc0e26d4------2</link>
            <guid isPermaLink="false">https://medium.com/p/d890a7ff6463</guid>
            <category><![CDATA[oauth]]></category>
            <category><![CDATA[oauth2]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[flutter-web]]></category>
            <category><![CDATA[flutter-app-development]]></category>
            <dc:creator><![CDATA[Guillaume Roux]]></dc:creator>
            <pubDate>Sat, 27 Feb 2021 12:12:24 GMT</pubDate>
            <atom:updated>2021-02-27T12:18:01.086Z</atom:updated>
            <content:encoded><![CDATA[<p>A few days ago I’ve published this article:</p><p><a href="https://rouxguillaume.medium.com/flutter-web-twitch-oauth2-authentication-flow-implementation-77d239c72be5">Flutter Web: Twitch OAuth2 authentication flow implementation</a></p><p>While it might have been helpful to some readers I have found a much better way to manage authentication on Flutter Web. Thanks to my friend and colleague <a href="https://medium.com/u/60b78a4555bb">Meï M.</a> for his help.</p><p>Usually when you are authenticating on a website or a web application using 3rd party services you connect your account through an external window. This process avoid the website you were on to reload entirely after the redirection. It is the main issue implied with the method described in my previous article.</p><p>So today let’s fix that and implement OAuth2 flow using an external authentication window. Still by using Twitch’s API so people who followed the previous article won’t be too lost.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*KynSC42Wrt_T_MdkYbxYfA.png" /></figure><h3>Prerequisites</h3><ul><li>Create your initial Flutter application</li><li>A valid Twitch and Twitch Developer account</li><li>Some basic HTML and JS knowledge</li></ul><h3>Set Up</h3><p>Before we start coding let me explain what will be our authentication flow. We are going to open Twitch authentication URL in a new window by passing as parameters in the URL our client_id and the redirect_uri . The main difference compared to the previous article is that our redirect_uri will not be used to redirect to our application after the authentication process but to a static HTML page which sole purpose will be to send to our main application window the data we are looking for. In this case it will be the OAuth token contained in our URL.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/771/1*sva8efiAVXYCmW-NM1BQ8w.png" /></figure><p>In terms of code the actions we want to accomplish are:</p><ul><li>Open <strong>Twitch Authentication Page</strong> in an <strong>External Window</strong></li><li>After authentication <strong>redirect to</strong> our <strong>Static Page</strong></li><li><strong>Send </strong>the URL containing the authentication token from our <strong>Static Page</strong> to the <strong>Main Page</strong></li><li>Catch the token in our <strong>Main Page</strong></li><li>Close the <strong>External Window</strong></li></ul><h3>Create our Static Page</h3><p>First thing to do before even trying to open the external window is to create the static HTML page we are going to be redirected to.</p><p>Here is the code for the static.html page that you will have to create inside the web/ folder:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d55563fd7faf429fd75e4a1416698027/href">https://medium.com/media/d55563fd7faf429fd75e4a1416698027/href</a></iframe><p>It is basically an empty HTML page, the important part is inside &lt;script&gt; . To be able to send our page’s URL containing our authentication token we are using the method postMessage() which allows us to communicate with our main window through the window.opener reference (<a href="https://developer.mozilla.org/fr/docs/Web/API/Window/postMessage">Window.postMessage doc</a>). We are sending the window.location.href , our URL containing the token.</p><h3>Open an External Window</h3><p>Now how to open the external window you may ask ? Well it is actually quite simple as we have an abstraction in the dart:html library:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e1f07266c0a9d4fad3f3f7be3161c182/href">https://medium.com/media/e1f07266c0a9d4fad3f3f7be3161c182/href</a></iframe><p>So to open our external window we call the method window.open and adding width and height options so it will open a new window instead of a tab. We are keeping the window’s reference inside our _popupWin variable so we will be able to close it programatically after we receive our access token.</p><p>The only thing that is left is to do is to add a listener to get the message sent by our external window.</p><h3>Listen to Message</h3><p>And once again Dart is well made as we have an abstraction for message listening:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2093e630fb8bae0b307d8e95db891ea9/href">https://medium.com/media/2093e630fb8bae0b307d8e95db891ea9/href</a></iframe><p>As you can see here we are relying on window.onMessage to listen to our postMessage from our static page. It is important to check beforehand if the event received contains access_token as the Twitch login itself sends a message to confirm that the authentication succeeded. And you just have to parse your URL, you have a working example here but you can do this however you want.</p><p>To end our authentication flow we close the window using our previously saved reference _popupWin .</p><h3>Conclusion</h3><p>And this is it ! I hope that the steps were clear and that you have understood them all. As for the previous article this should work with any OAuth2 flow. This works in a much more “web” manner without needing to reload your application entirely. This is the last time I will bother you with authentication in Flutter Web as I consider this solution much more final and useable.</p><p>If this article was helpful to you then do not hesitate to leave a comment, share it or clap so it might help more people.</p><p>As always here is the source code I have based my article on:</p><p><a href="https://github.com/TesteurManiak/flutter_web_twitch_auth/tree/use-external-win">TesteurManiak/flutter_web_twitch_auth</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d890a7ff6463" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/flutter-web-oauth-authentication-through-external-window-d890a7ff6463">Flutter Web: OAuth authentication through external window</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[Flutter Web: Twitch OAuth2 authentication flow implementation]]></title>
            <link>https://itnext.io/flutter-web-twitch-oauth2-authentication-flow-implementation-77d239c72be5?source=rss-7e7bfc0e26d4------2</link>
            <guid isPermaLink="false">https://medium.com/p/77d239c72be5</guid>
            <category><![CDATA[oauth]]></category>
            <category><![CDATA[flutter-web]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[oauth2]]></category>
            <category><![CDATA[twitch]]></category>
            <dc:creator><![CDATA[Guillaume Roux]]></dc:creator>
            <pubDate>Sat, 20 Feb 2021 20:37:10 GMT</pubDate>
            <atom:updated>2021-02-20T22:05:17.137Z</atom:updated>
            <content:encoded><![CDATA[<p>Currently, it is difficult to implement authentication flow such as OAuth2 on Flutter Web. One of the difficulties encountered is to be able to grab the authentication token from the URL redirection as you don’t have access to URL listener or any ways to follow changes inside an external Navigation tab.</p><p>I had a hard time figuring how to implement this kind of authentication flow while a solution was actually quite simple. Today I am going to explain it using Twitch’s API as an example.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/618/1*x1g4gFCUFOFY1Suun8sH-g.jpeg" /></figure><h3>Prerequisites</h3><ul><li>Create your initial Flutter Web application</li><li>Having a valid Twitch and Twitch Developer account</li></ul><h3>Set Up</h3><p>First of all, you will need to register an app in the <a href="https://dev.twitch.tv/console">Twitch Developer Console</a> you can name it however you want and you will define as a redirection URL: <a href="http://localhost:8080/">http://localhost:8080</a> (or any other port you want). I am using a localhost URL as the example will not be hosted online but feel free to also add the URL on which your application will be deployed.</p><p>Now that you have created your application on the console you should have obtained a client id. You can already copy-paste it inside your main.dart .</p><pre>const String clientId = &quot;YOUR_CLIENT_ID&quot;;</pre><h3>Manage the authentication flow</h3><p>Now that you have your client id the next step is to fetch the authentication token from Twitch’s API using <a href="https://dev.twitch.tv/docs/authentication/getting-tokens-oauth#oauth-implicit-code-flow">OAuth implicit code flow</a>.</p><p>By referring to the documentation the code is returned inside the redirect URL after identification and authorization.</p><p>If you did not change the structure of the base project generated by Flutter you should have a MyHomePage class which is a StatefulWidget . Inside the class _MyHomePageState you are going to add a variable :</p><pre>/// To save the OAuth2 token.<br>String _token;</pre><p>Now you are going to implement the initState method inside your widget’s State :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/57604450968b32d259010cf3692d5d32/href">https://medium.com/media/57604450968b32d259010cf3692d5d32/href</a></iframe><p>Let me explain what is happening inside this method.</p><p>First things first, you are grabbing the current URL of your Flutter application using Uri.base . Then by referring to Twitch’s documentation you know that the token is returned in the URL as follows: https://&lt;your registered redirect URI&gt;#access_token=&lt;an access token&gt; which means that if your URL contains access_token= you have been redirected by Twitch.</p><p>Now you have 2 scenarios:</p><ol><li>You don’t have the token inside your URL so, you need to authenticate. To do this we are using the dart:htmlmethod window.location(url) which will change the URL of our current tab to the Twitch authorization.</li><li>You have been redirected by Twitch and have the token inside your URL so you need to parse it to get your token and save it inside your _token variable.</li></ol><h3>Launch the app</h3><p>At this point you should be ready, you can add a print(token) to ensure it has been correctly fetched and you simply need to run the command :</p><pre>flutter run -d chrome --web-port=8080</pre><p>This command will run your application inside a Google Chrome browser and the added parameter will force the host on localhost port 8080. Note that I am using chrome, but it also works with any other compatible argument you might pass. And of course, if you have registered a port different than 8080 in the Twitch Developer Console then use this one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*_lbg3DKQuVhkq8Ao6xiNYw.gif" /><figcaption>Result</figcaption></figure><h3>Conclusion</h3><p>And here it is you are done ! As you can see it is actually quite easy, the redirection does everything for you and Flutter is smart enough to not be disturbed by the fact of having the authorization response inside its URL. By using this implementation you don’t need any WebView or listening to URL changes. While it might not work for every use case I think it can still help a lot.</p><p>You can find the entire source code I used for this article here :</p><p><a href="https://github.com/TesteurManiak/flutter_web_twitch_auth">TesteurManiak/flutter_web_twitch_auth</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=77d239c72be5" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/flutter-web-twitch-oauth2-authentication-flow-implementation-77d239c72be5">Flutter Web: Twitch OAuth2 authentication flow implementation</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[Flutter: FCM notifications on Mobile and Web]]></title>
            <link>https://rouxguillaume.medium.com/flutter-fcm-notifications-on-mobile-and-web-3a23db50d61d?source=rss-7e7bfc0e26d4------2</link>
            <guid isPermaLink="false">https://medium.com/p/3a23db50d61d</guid>
            <category><![CDATA[notifications]]></category>
            <category><![CDATA[firebasecloudmessaging]]></category>
            <category><![CDATA[flutter-web]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[fcm]]></category>
            <dc:creator><![CDATA[Guillaume Roux]]></dc:creator>
            <pubDate>Thu, 15 Oct 2020 07:56:40 GMT</pubDate>
            <atom:updated>2020-10-15T07:56:40.017Z</atom:updated>
            <content:encoded><![CDATA[<p>I have been recently asked to add notifications to a Flutter application I was working on. Problem is, we also had to make it available for the on development Flutter Web application. As it was an interesting challenge I decided to share how I have done my implementation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/938/1*y7pEGBne39DTLnugCfruXg.png" /></figure><h3>Prerequisites</h3><ul><li>Create your initial Flutter app</li><li>Having created and configured a <a href="https://console.firebase.google.com/u/0/">Firebase project</a> for Android (or iOS) &amp; Web</li></ul><h3>Set Up</h3><p>First of all we are going to add Firebase to our Flutter application for mobile and web :</p><ul><li>Open your pubspec.yaml and add the following dependencies:</li></ul><pre>dependencies:<br>   firebase_core: ^0.5.0+1 # Works for both mobile &amp; web<br>   firebase_messaging: ^7.0.2 # This one is for the mobile part</pre><p><em>Do not forget to execute </em><em>flutter packages get afterwards.</em></p><ul><li>Initialize your Firebase in your lib/main.dart :</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f289738d6e1cef3af8ed1bd12944ee1c/href">https://medium.com/media/f289738d6e1cef3af8ed1bd12944ee1c/href</a></iframe><ul><li>As you want your Firebase to be web compatible you also need to edit your web/index.html :</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d3ddbcc60cce7d8649631686dbdf2ca4/href">https://medium.com/media/d3ddbcc60cce7d8649631686dbdf2ca4/href</a></iframe><ul><li>Add your Firebase credential files to your mobile application. For Android I needed to add thegoogle-services.json downloaded from Firebase inside android/app/ . Also do not forget to add the necessary Android dependencies inside your android/build.gradle and android/app/build.gradle (you can refer to Firebase documentation to do so).</li></ul><h3>Coding classes for mobile &amp; web</h3><p>Now that your Firebase is ready to be used we can begin to code our implementation for both mobile &amp; web.</p><p>We will first create an abstract class called NotificationHelper which will define our needed methods and a class NotificationModel which correspond to the parsed notification we will received from Firebase.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/465d75c6750676a4587311d11a6defcf/href">https://medium.com/media/465d75c6750676a4587311d11a6defcf/href</a></iframe><p>So, to explain a bit what method are defined by NotificationHelper :</p><ul><li>Future&lt;bool&gt; get isReady; this getter will help us to know if our singleton (which I will present after) has been correctly initialized.</li><li>Stream&lt;NotificationModel&gt; get stream; this getter will be used to be notified when we receive a new notification from Firebase.</li><li>void close(); is used to close our stream.</li><li>Future&lt;String&gt; getToken([bool force = false]); this method will allow us to fetch our FCM token from Firebase. If the token has already been fetched it will return it except if the force parameter is at true .</li><li>Future&lt;dynamic&gt; requestPermission(); is the method used to request notification permissions (push notification for mobile and browser notification on web).</li></ul><p>Now that we have defined our method we can create our classes FirebaseMessagingHelper . Both will be singletons to avoid creating multiple instances and requesting more than one time the FCM token or listen to unused notifications stream.</p><h4>Mobile implementation</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/da845533c179e2646e0595f424270250/href">https://medium.com/media/da845533c179e2646e0595f424270250/href</a></iframe><p>This implementation relies on the package firebase_messaging which will configure a callback method _onMessageReceived when a notification is catched. This callback will update our stream so we can be notified when a new event is send.</p><h4>Web implementation</h4><p>As the package firebase_messaging is currently not compatible with Flutter Web we first need to register a service worker in the index.html .</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4bbd07bc76d5b8dd702ea20dec059928/href">https://medium.com/media/4bbd07bc76d5b8dd702ea20dec059928/href</a></iframe><p>We have uncommented the firebase-messaging script and added :</p><pre>&lt;script&gt;<br>   if (&#39;serviceWorker&#39; in navigator) {<br>      window.addEventListener(&#39;load&#39;, function () {<br>         navigator.serviceWorker.register(&quot;/firebase-messaging-sw.js&quot;);<br>      });<br>   }<br>&lt;/script&gt;</pre><p>And as precised in the index we are creating a file named firebase-messaging-sw.js inside the /web folder (it is important to keep this name as Firebase will look for it).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef491b22c9c4f98d7cb9b1515344ea05/href">https://medium.com/media/ef491b22c9c4f98d7cb9b1515344ea05/href</a></iframe><p>If you are using Firebase version’s different from mine try to keep the same for both index.html and firebase-messaging-sw.js .</p><p>Now we can code our FirebaseMessagingHelper class for web inside a firebase_web_messaging.dart .</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f705eec628db55bf238076af55831a32/href">https://medium.com/media/f705eec628db55bf238076af55831a32/href</a></iframe><p>The PUBLIC_API_KEY I am refering to can be found in your Firebase project’s settings -&gt; Cloud Messaging tab -&gt; Web Configuration. You might need to generate a key if you did not already have one.</p><h4>Implementing our classes</h4><p>As you have seen above both of our files firebase_mobile_messaging.dart &amp; firebase_web_messaging.dart have the same class so how can we use them together ?</p><p>My answer is : conditional import ! I am simply encapsulating my class in another abstract class which I will name NotificationEncapsulation to get the correct instance of FirebaseMessagingHelper depending of the platform.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c81abf62086bae8ccb149c687ea8561/href">https://medium.com/media/9c81abf62086bae8ccb149c687ea8561/href</a></iframe><p>Here is a home_page.dart file example which will fetch the FCM token and display it, working both on mobile and web.</p><p>You can test the notification using a site like <a href="https://www.apnstester.com/fcm/">Push Tester</a> to send notification to your application. Simply mention your server key which you can find in your Firebase project’s settings -&gt; Cloud Messaging Tab (should be the first section) and your application’s FCM token.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e3484826904e7917b448c166a926de5d/href">https://medium.com/media/e3484826904e7917b448c166a926de5d/href</a></iframe><h3>Conclusion</h3><p>Here it is folks ! Hope I was clear enough in explaining how to implement those push notifications (This is my first Medium story by the way). Do not hesitate to point out if some explanations were not with enough precision.</p><p>Firebase Messaging really is a useful tool but documentation and complete example for Flutter Web were missing so this is why I decided to make one myself.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3a23db50d61d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>