<?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 Endre Vekony on Medium]]></title>
        <description><![CDATA[Stories by Endre Vekony on Medium]]></description>
        <link>https://medium.com/@endrevekony?source=rss-db2696106421------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ou7bORv0mY3fIsobHTCUHA.jpeg</url>
            <title>Stories by Endre Vekony on Medium</title>
            <link>https://medium.com/@endrevekony?source=rss-db2696106421------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 13:11:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@endrevekony/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[Top 3 ways to view code coverage in Flutter]]></title>
            <link>https://endrevekony.medium.com/top-3-ways-to-view-code-coverage-in-flutter-1a20ac7d8297?source=rss-db2696106421------2</link>
            <guid isPermaLink="false">https://medium.com/p/1a20ac7d8297</guid>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[code-coverage]]></category>
            <category><![CDATA[csv-file]]></category>
            <category><![CDATA[lcov]]></category>
            <category><![CDATA[dart]]></category>
            <dc:creator><![CDATA[Endre Vekony]]></dc:creator>
            <pubDate>Thu, 24 Nov 2022 22:32:19 GMT</pubDate>
            <atom:updated>2022-11-24T22:32:19.982Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nSDJOuB24s9dVdLaMKPIvg.png" /></figure><p>Code coverage is a must have when you are writing a Flutter app.</p><p>Code coverage is a software testing metric that determines the number of lines of code that were successfully verified as part of a testing procedure, helping you analyze how comprehensively your software is verified.</p><p>Developing enterprise-grade software products is the ultimate objective of any software company. In order to achieve this goal however, companies must ensure that their software products meet all important quality criteria such <strong>correctness, reliability, effectiveness and security</strong>. These are only achievable through thorough testing.</p><p>The first command, to generate code coverage is:</p><pre>flutter test --coverage</pre><h3><strong>First Method — Inside IDE with Extensions</strong></h3><p>The first and simpliest method is to check the coverage in your preferred IDE, but I think we can do better than this.</p><p>For Android Studio developers, install <a href="https://plugins.jetbrains.com/plugin/12693-flutter-enhancement-suite">Flutter Enhancement Suite</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*R-rtVlAbZpFC26X53HHOLw.png" /></figure><p>For VSCode developers, install <a href="https://marketplace.visualstudio.com/items?itemName=Flutterando.flutter-coverage">Flutter Coverage</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lBSi-P2ayA_2YCvLKwaKcQ.png" /></figure><p>These extensions will show exactly which line is covered, partially covered, or not covered at all.</p><p>The biggest con is, these run only in your preferred IDE, and you cannot export them, or save them anywhere. So I think the other options are way better.</p><h3><strong>Second Method — LCOV</strong></h3><p><strong>LCOV</strong> is an extension of GCOV, a GNU tool that provides information about which parts of a program are actually covered during execution of a particular test case.</p><p>So let’s install LCOV</p><p>on Linux devices:</p><pre>sudo apt-get install lcov</pre><p>On OSX using HomeBrew:</p><pre>brew install lcov</pre><p>On Windows using Chocolatey:</p><pre>choco install lcov</pre><p>After successful installation of lcov we should generate our coverage in HTML view, with the next commands:</p><pre>genhtml coverage/lcov.info</pre><p>Then head to coverage/html/index.html, and open it in your browser, and there you have your interactive code coverage.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-geH22edv3bGxCze-2TRfA.png" /></figure><h3>Third Method — CSV</h3><p>In my opinion this is the best way to view coverage, because it is easy, and you can give the .csv file to anyone, and they will understand how much of your code is covered.</p><p>first we want to import <a href="https://pub.dev/packages/test_cov_console">https://pub.dev/packages/test_cov_console</a> to our pubspec.yaml, or simply add it with:</p><pre>dart pub global activate test_cov_console</pre><p>Then run:</p><pre>flutter pub run test_cov_console --file=coverage/lcov.info --multi -c</pre><p>Then open coverage/test_cov_console.csv, with your preferred CSV Editor.</p><p>As I am using OSX, I open it in Numbers app, and here is the result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*85xkJ_wUFuWWn8tD11suaA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nSDJOuB24s9dVdLaMKPIvg.png" /></figure><p>I hope my methods help you find your favorite ways to read code coverage and consider using them in your best projects.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1a20ac7d8297" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Flutter change AppIcon on runtime]]></title>
            <link>https://endrevekony.medium.com/flutter-change-appicon-on-runtime-9a047fb80332?source=rss-db2696106421------2</link>
            <guid isPermaLink="false">https://medium.com/p/9a047fb80332</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[icons]]></category>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[flutter]]></category>
            <dc:creator><![CDATA[Endre Vekony]]></dc:creator>
            <pubDate>Tue, 20 Sep 2022 13:21:38 GMT</pubDate>
            <atom:updated>2022-09-20T13:21:38.767Z</atom:updated>
            <content:encoded><![CDATA[<p>Currently this is only an iOS feature, but I think it is really amazing, so lets give it a try.</p><p>I am using the default Flutter logo collection from: <a href="https://flutter.dev/brand"><strong>https://flutter.dev/brand</strong></a><strong> .</strong></p><p>If you need image generation, I would highly recommend: <a href="https://makeappicon.com/"><strong>https://makeappicon.com/</strong></a> ,it is easy, and free.</p><p>In first step I am gonna create a folder named <strong>assets</strong>, then addig the icons, to the <strong>pubspec.yaml</strong>.</p><pre>flutter:<br>  uses-material-design: true<br>  assets:<br>     - assets/icon_flutter.png<br>     - assets/icon_flutter_dk-blue.png<br>     - assets/icon_flutter_wht.png</pre><p>Then hit <strong>flutter pub get </strong>and we are ready to add the Icon, in the XCode.</p><p>Let’s open the ios/Runner.xcodeproj in your XCode, then navigate to Runner/assets, and add just drag in the Icon packs:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NzZ9EX8B_-JX1PDi6kuDrw.png" /></figure><p>Then go to <strong>Build Settings</strong> tab and filter for Icons</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ff5CYezMnqQcZvaWoc91TQ.png" /></figure><p>Enable Alternate Icons, and add the names of the Icon sets</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zswtmsmXwb18p7ymuLnYtg.png" /></figure><p>Then let’s write an example code here:</p><pre>import &#39;package:flutter/material.dart&#39;;<br>import &#39;package:variable_app_icon/variable_app_icon.dart&#39;;<br><br>void main() =&gt; runApp(const MyApp());<br><br>class MyApp extends StatelessWidget {<br>  const MyApp({super.key});<br><br>  @override<br>  Widget build(BuildContext context) {<br>    return MaterialApp(<br>      title: &#39;App Icon Demo&#39;,<br>      home: Scaffold(<br>        backgroundColor: Colors.black26,<br>        appBar: AppBar(<br>          title: const Text(&#39;App Icon Demo&#39;),<br>        ),<br>        body: Center(<br>          child: Column(<br>            mainAxisAlignment: MainAxisAlignment.spaceEvenly,<br>            children: [<br>              GestureDetector(<br>                onTap: () async =&gt; await VariableAppIcon.changeAppIcon(&#39;&#39;),<br>                child: Image.asset(<br>                  &#39;assets/icon_flutter.png&#39;,<br>                  height: 50,<br>                  width: 50,<br>                ),<br>              ),<br>              GestureDetector(<br>                onTap: () async =&gt; await VariableAppIcon.changeAppIcon(&#39;AppIcon 1&#39;),<br>                child: Image.asset(<br>                  &#39;assets/icon_flutter_dk-blue.png&#39;,<br>                  height: 50,<br>                  width: 50,<br>                ),<br>              ),<br>              GestureDetector(<br>                onTap: () async =&gt; await VariableAppIcon.changeAppIcon(&#39;AppIcon 2&#39;),<br>                child: Image.asset(<br>                  &#39;assets/icon_flutter_wht.png&#39;,<br>                  height: 50,<br>                  width: 50,<br>                ),<br>              ),<br>            ],<br>          ),<br>        ),<br>      ),<br>    );<br>  }<br>}</pre><p>And here is the Magic, run the app, and press one of the buttons:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/919/1*isvtD-ux7gMfi4m3rVIkYw.png" /></figure><p>I hope you enjoyed this article. If it was interesting a clap, follow or comment will be appreciated.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9a047fb80332" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[BlocObserver in Bloc 8]]></title>
            <link>https://endrevekony.medium.com/blocobserver-in-bloc-8-2977803b851?source=rss-db2696106421------2</link>
            <guid isPermaLink="false">https://medium.com/p/2977803b851</guid>
            <category><![CDATA[flutter-bloc]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[bloc]]></category>
            <category><![CDATA[observer]]></category>
            <category><![CDATA[dart]]></category>
            <dc:creator><![CDATA[Endre Vekony]]></dc:creator>
            <pubDate>Wed, 08 Jun 2022 12:13:44 GMT</pubDate>
            <atom:updated>2022-06-13T04:55:35.055Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/862/1*k2SAhuYZi_l58KcvXmq5Pg.png" /><figcaption>Flutter Bloc versio 8.0</figcaption></figure><p>Since the most of us switched into BLoC 8, I didn’t see anybody talking about the major change in <strong>BlocObserver().</strong></p><p>I think developers, who uses BLoC as their main state management in Flutter rarely used this class, but it was always there for us :)</p><p>Soo lets get back to the beginning, what is BlocObserver, and why we actually need it?</p><p>BlocObserver helps you identify the current states of your BLoCs and Cubits, and react to state changes. Also BlocObserver runs in a separate zone, so if you want to react to some change with some futures, or caching in the background, I would prefer this. Also you can spawn an Isolate in the Zone for your hard calculations, and not jank the app, and you can still run at 60fps.</p><p>This is great isn’t it? So why we dont use this more often?</p><ul><li>I think the biggest reason is, because it is not listed or documented at the <a href="https://pub.dev/packages/flutter_bloc">pub dev page of flutter_bloc</a>.</li></ul><p>I can show a lot of examples of usefulness of BlocObserver before BLoC 8.0, but that would be a waste of time, so lets jump straight into the point.</p><h4><strong>And how we use it? Let me show you an example</strong></h4><p>First lets jump into our main.dart</p><pre>void main() {<br>  BlocOverrides.runZoned(<br>    () =&gt; runApp(<br>      const MyApp(),<br>    ),<br>    blocObserver: AppBlocObserver(),<br>  );<br>}</pre><p>This will run our app in the Zone. And let me show you what AppBlocObserver contains.</p><pre>class AppBlocObserver extends BlocObserver {<br>  <br><strong>  ///We can run something, when we create our Bloc<br></strong>  @override<br>  void onCreate(BlocBase bloc) {<br>    super.onCreate(bloc);<br><strong>    ///We can check, if the BlocBase is a Bloc or a Cubit<br></strong>    if (bloc is Cubit) {<br>      print(&quot;This is a Cubit&quot;);<br>    } else {<br>      print(&quot;This is a Bloc&quot;);<br>    }<br>  }<br><br><strong>  ///We can react to events<br></strong>  @override<br>  void onEvent(Bloc bloc, Object? event) {<br>    super.onEvent(bloc, event);<br>    print(&quot;an event Happened in $bloc the event is $event&quot;);<br>  }<br><br><strong>  ///We can even react to transitions<br></strong>  @override<br>  void onTransition(Bloc bloc, Transition transition) {<br>    super.onTransition(bloc, transition);<br><strong>    /// With this we can specifically know, when and what changed in our Bloc<br></strong>    print(&quot;There was a transition from ${transition.currentState} to ${transition.nextState}&quot;);<br>  }<br><br>  <strong>///We can react to errors, and we will know the error and the StackTrace</strong><br>  @override<br>  void onError(BlocBase bloc, Object error, StackTrace stackTrace) {<br>    super.onError(bloc, error, stackTrace);<br>    print(&quot;Error happened in $bloc with error $error and the stacktrace is $stackTrace&quot;);<br>  }<br><br><strong>  ///We can even run something, when we close our Bloc<br></strong>  @override<br>  void onClose(BlocBase bloc) {<br>    super.onClose(bloc);<br>    print(&quot;BLOC is closed&quot;);<br>  }<br>}</pre><h3><strong>BlocObserver after BloC 8.0</strong></h3><p>BlocObserver went through a lot of changes. the first thing is, it is now an abstract class, so it cannot be instantiated.</p><p>The main thing is, we can now use multiple Observers. This improves the quality, robustness and readability of our code, and gives us more flexibility, to do these things on specific Blocs. Even change the Observer at runtime, which is a really great thing.</p><p>We can also nest an Observer into an Observer. So one one Bloc can send data to AppBlocObserver1, while other Blocs can send to AppBlocObserver2.</p><p>I will show you how fun it is, but I cannot give you a real life problem, where I would nest them into each other. ( If anybody can, please comment)</p><pre>void main() {<br>  BlocOverrides.runZoned(<br>    () {<br>      <strong><em>/// We can get the current Observer like this</em></strong><em><br>      </em>final overrides = BlocOverrides.current;<br><strong>      <em>/// Blocs in this zone report to BlocObserverA<br></em></strong><em>      </em>BlocOverrides.runZoned(<br>        () {<br><em>          </em>final overrides = BlocOverrides.current;<br>          <strong><em>/// Blocs in this zone report to BlocObserverB</em></strong><em><br>        </em>},<br>        blocObserver: BlocObserverB(),<br>      );<br><br>    },<br>    blocObserver: BlocObserverA(),<br>  );<br>}</pre><p>Also there is a small change. Now we can call errors from a<strong> BlocBase </strong>and it will not be threatened as an uncaught exception, but our <strong>onError</strong> method will print it out to the console. Here is a small example</p><pre>@override<br>void onCreate(BlocBase bloc) {<br>  super.onCreate(bloc);<br>  if (bloc is Cubit) {<br>    bloc.addError(&#39;Why do we use a Cubit, We should use Bloc&#39;);<br>  } else {<br>    print(&quot;This is a Bloc&quot;);<br>  }<br>}</pre><p>So BloC fanatics, I hope you enjoyed this article, and think of using Observers for your next project. If it was interesting a clap or comment will be appreciated.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2977803b851" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Coding Guidelines in Flutter]]></title>
            <link>https://endrevekony.medium.com/code-guidelines-in-flutter-d72680986e24?source=rss-db2696106421------2</link>
            <guid isPermaLink="false">https://medium.com/p/d72680986e24</guid>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[mistakes]]></category>
            <category><![CDATA[beginner]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[guidelines]]></category>
            <dc:creator><![CDATA[Endre Vekony]]></dc:creator>
            <pubDate>Wed, 16 Mar 2022 12:47:18 GMT</pubDate>
            <atom:updated>2022-04-04T08:14:55.138Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dM0Muo-lysK0vMbqqP2utA.png" /></figure><p>This coding guideline is made to help Flutter developers improve their code style and the overall performance of their apps.</p><h3>1. If possible use Constants</h3><p>The const keyword is <strong>used when the value of the variable is known at compile-time and never changes</strong>.</p><p>This includes const Classes, variables, Lists etc…</p><h3>2. Use nullable if you really need it</h3><p>If you are sure, that you need to initialize the variable at runtime, use<strong> late.</strong></p><p>Nullable types are great but use it only, when you are sure, that the variable can, or will be null.</p><h3>3. Split large widgets into smaller widgets.</h3><p>You should try to <strong>minimize the size of your build function</strong> and keep widgets as small and modular as possible. <strong>Stateful widgets are being redrawn when there is a state change</strong>, which means that the entire widget will have to be rebuild, the <strong>larger the build tree</strong>, the <strong>more resources</strong> it takes to be rebuilt.</p><h3>4. Consider using =&gt; for short functions and methods</h3><p>But only use =&gt; when everything, including the function declaration, fits in ± 120 characters.</p><p>Also it is useful for inline callbacks that just return list or map literals.</p><h3>5. Consistent imports</h3><p>Importing relative path or package doesn’t affect the performance. But if you import something as relative, then a package, and another relative path, then it will confuse you, and you can easily find yoursellf in a big mess.</p><p>It is not important which one you choose, but make it consistent.</p><p>I prefer to use packages instead of relative paths.</p><h3>6. InitState and Dispose order</h3><p>The code will work either way, but the Flutter documentation says, the correct order is, first call <strong>super.initState()</strong> and last call <strong>super.dispose()</strong></p><p><em>//GOOD EXAMPLE</em></p><pre>@override<br>void initState() {<br>  super.initState();    <br>  // DO YOUR STUFF<br>}<br><br>@override<br>void dispose() {<br>  // DO YOUR STUFF<br>  super.dispose();<br>}</pre><h3>7. Use Finals</h3><p>“final” means <strong>single-assignment</strong>: a final variable or field must have an initializer. Once assigned a value, a final variable’s value cannot be changed.</p><p><em>//BAD EXAMPLE</em></p><pre>List&lt;String&gt; toppings = [&#39;mozarella&#39;, &#39;pepperoni&#39;];<br>toppings = [&#39;olives&#39;, &#39;cheddar&#39;];</pre><p><em>//GOOD EXAMPLE</em></p><pre>final List&lt;String&gt; toppings = [&#39;mozarella&#39;, &#39;pepperoni&#39;];<br>toppings.clear()<br>toppings.addAll([&#39;olives&#39;, &#39;cheddar&#39;]);</pre><h3>8. Efficiently use Operators</h3><p>This way you can avoid logical errors more easily, and your code will be more robust and readable.</p><p><em>//BAD EXAMPLE</em></p><pre>String? pizza;<br>pizza == null ? &#39;margherita&#39; : pizza;</pre><p><em>//GOOD EXAMPLE</em></p><pre>String? pizza;<br>pizza ?? &#39;margherita&#39;;</pre><h3>9. Return Widgets instead of Widget returning functions</h3><p>This is a big anti-pattern. When we make a change in and refresh the entire widget, it will also refresh the widgets that we have within the method, which leads us to waste CPU cycles. Instead export the widgets to a new Stateless Widgets.</p><p>Another bad point, if we pass the BuildContext as a parameter. If we need to pass the context, that is usually a red flag, that we need a new Widget for this.</p><p><em>//BAD EXAMPLE</em></p><pre>@override<br>Widget build(BuildContext context) {<br>  return Padding(<br>    padding: const EdgeInsets.all(8.0),<br>    child: _buildPizzaWidget(context),<br>  );<br>}<br><br>Widget _buildPizzaWidget(BuildContext context) {<br>  return Center(<br>    child: Text(<br>      context.read&lt;PizzaCubit&gt;().phoneNumber,<br>    ),<br>  );<br>}</pre><p><em>//GOOD EXAMPLE</em></p><pre>@override<br>  Widget build(BuildContext context) {<br>    return const Padding(<br>      padding: const EdgeInsets.all(8.0),<br>      child: PizzaWidget(),<br>    );<br>  }<br>}<br><br>class PizzaWidget extends StatelessWidget {<br>  const PizzaWidget({Key? key}) : super(key: key);<br><br>  @override<br>  Widget build(BuildContext context) {<br>    return Center(<br>      child: Text(<br>        context.read&lt;PizzaCubit&gt;().phoneNumber,<br>      ),<br>    );<br>  }<br>}</pre><p>As we can see, we don’t pass the BuildContext, and at the and our whole Widget is a constant, while on the other hand, a Widget returning function can never be a constant.</p><h3>10. Annotate everything if you can</h3><p>This includes variables, the data you receive from an endpoint, and even the return of your function.</p><p><em>//BAD EXAMPLE</em></p><pre><em>//BAD, we need to annotate the return type<br></em>makePizza(String lastTopping) =&gt; &#39;The last is: $lastTopping&#39;;</pre><p><em>//GOOD EXAMPLE</em></p><pre><em>//The return type is String<br></em>String makePizza(String lastTopping) =&gt; &#39;The last is: $lastTopping&#39;;</pre><h3>11. No need to write default arguments</h3><p>This is a simple one, if you doesn’t need it, don’t write it.</p><p><em>//BAD EXAMPLE</em></p><pre>@override<br>Widget build(BuildContext context) {<br>  return const Padding(<br>    padding: EdgeInsets.only(<br>      left: 5.0,<br>      <em>//We dont need this<br>      </em>right: 0,<br>    ),<br>  );<br>}</pre><p><em>//GOOD EXAMPLE</em></p><pre>@override<br>Widget build(BuildContext context) {<br>  return const Padding(<br>    padding: EdgeInsets.only(left: 5.0),<br>  );<br>}</pre><h3>12. Avoid unnecessary nesting functions</h3><p>It just makes the code unreadable, and () =&gt; is just and extra function, that you doesn’t need at all. Also as mentioned before, passing the context as argument is unnecessary here.</p><p><em>//BAD EXAMPLE</em></p><pre>@override<br>Widget build(BuildContext context) {<br>  return GestureDetector(<br>     onTap: () =&gt; orderPizza(context),<br>  );<br>}<br><br>orderPizza(BuildContext context) {<br>  context.read&lt;PizzaCubit&gt;().orderPizza;<br>}</pre><p><em>//GOOD EXAMPLE</em></p><pre>@override<br>Widget build(BuildContext context) {<br>  return GestureDetector(<br>    onTap: context.read&lt;PizzaCubit&gt;().orderPizza();<br>  );<br>}</pre><h3>13. Use SizedBox.shrink() instead of the empty Containers()</h3><p>This doesn’t need much explanation. If you want to show nothing <strong>Container() </strong>is a wrong way. <strong>SizedBox.shrink() </strong>takes as less space, ass possible.</p><h3>14. ListView instead of SingleChildScrollView and Column combination</h3><p>If you want to show your list of widgets, you usually put it into a<strong> Column(). </strong>Then you want your Widgets to be responsive and scrollable, now you put it into an Expanded or Flexible Widget, and wrap it with a <strong>SingleChildScrollView().</strong></p><p>Well, good news. ListView does all of these things for you. It is more performant, and way less code for you.</p><p><em>//BAD EXAMPLE</em></p><pre>@override<br>Widget build(BuildContext context) {<br>  return SingleChildScrollView(<br>    child: Expanded(<br>      child: Column(children: [<br>        <em>//YOUR WIDGETS<br>      </em>],),<br>    ),<br>  );<br>}</pre><p><em>//GOOD EXAMPLE</em></p><pre>@override<br>Widget build(BuildContext context) {<br>  return ListView(<br>    children: [<br>      <em>//YOUR WIDGETS<br>    </em>],<br>  );<br>}</pre><h3>15. ListView.builder() for long lists</h3><p>If you have 10 Widgets in your List, you can go for a simple ListView. But what if you have 10000 Widgets?</p><p>The <strong>ListView()</strong> constructor requires us to create all items at once. This is good when list items are less and all will be seen on the screen.</p><p>The <strong>ListView.builder()</strong> constructor will create items as they are scrolled onto the screen like on-demand. This is the best practice for development for List widget where items will only render only when items are going seen on the screen.</p><h3>16. Pass only the params you need</h3><p>Instead of passing a class with a bunch of data, you should make it simple, and pass the param that you actually need</p><p>Lets pretend we have a Pizza class</p><pre>class Pizza {<br>  int inches = 30;<br>  String sauce = &#39;Tomato&#39;;<br>  bool pepperoni = true;<br>  bool mushrooms = false;<br>  String cheese = &#39;Cheddar&#39;;<br>}</pre><p>If my Widget only cares about the dough, why should I pass the toppings?</p><p><em>//BAD EXAMPLE</em></p><pre>class PizzaDoughWidget extends StatelessWidget {<br>  final Pizza pizza;<br>  const PizzaDoughWidget({Key? key, required this.pizza}) : super(key: key);<br><br>  @override<br>  Widget build(BuildContext context) {<br>    return Text(pizza.doughSize.toString());<br>  }<br>}</pre><p><em>//GOOD EXAMPLE</em></p><pre>class PizzaDoughWidget extends StatelessWidget {<br>  final int doughSize;<br>  const PizzaDoughWidget({Key? key, required this.doughSize}) : super(key: key);<br><br>  @override<br>  Widget build(BuildContext context) {<br>    return Text(doughSize.toString());<br>  }<br>}</pre><p>This is tricky when you need to compare, how much params do you need, and should you pass the whole class. My advice is, if you need more than two params from the same class, you can pass the whole class, because it will be less code, and more readable at the end.</p><h3>17. Avoid switch case</h3><p>There are some good uses for the switch case, but most of the time, a simple Map will do a better job.</p><p>Lets go back to our Pizza example:</p><p><em>//BAD EXAMPLE</em></p><pre>List&lt;String&gt; getPizzaFromName(String name) {<br>  switch (name) {<br>    case &#39;pepperoni&#39;:<br>      return [&#39;cheese&#39;, &#39;tomato&#39;, &#39;pepperoni&#39;];<br>    case &#39;hawaii&#39;:<br>      return [&#39;cheese&#39;, &#39;tomato&#39;, &#39;pineapple&#39;];<br>    default :<br>      return [&#39;cheese&#39;, &#39;tomato&#39;];<br>  }<br>}</pre><pre>List&lt;String&gt; toppings = getPizzaFromName(&#39;pepperoni&#39;);</pre><p><em>//GOOD EXAMPLE</em></p><pre>Map&lt;String, List&lt;String&gt;&gt; getPizzeFromName = {<br>  &#39;pepperoni&#39; : [&#39;cheese&#39;, &#39;tomato&#39;, &#39;pepperoni&#39;],<br>  &#39;hawaii&#39; : [&#39;cheese&#39;, &#39;tomato&#39;, &#39;pineapple&#39;],<br>};</pre><pre>List&lt;String&gt; toppings = getPizzeFromName[&#39;pepperoni&#39;];</pre><p>As we can see, this less code, and it is easier to add another Map item, than another switch case.</p><h3>18. Prefer using _ for unused callback Parameters</h3><p>Sometimes the type signature of a callback function requires a parameter, but the callback implementation doesn’t <em>use</em> the parameter. In this case, it’s idiomatic to name the unused parameter _. If the function has multiple unused parameters, use additional underscores to avoid name collisions: __, etc.</p><h3>19. Prefer for instead of forEach</h3><p>For is way better than forEach. First it is more performance friendly, and secondly, you have access to return, break, continue, and the access to the index is also useful.</p><p><em>//BAD EXAMPLE</em></p><pre>pizzaToppings.forEach((topping) {<br>  print(topping);<br>});</pre><p><em>//GOOD EXAMPLE</em></p><pre>for (var topping in pizzaToppings) {<br>  print(topping);<br>}</pre><h3><strong>20. Return Widget instead of specific Widget type</strong></h3><p>Yeah, we should always be type specific, but not this time. Imagine if we want to wrap the CustomWidget into a simple <strong>Padding(). </strong>Our code will break, and we can spend a lot of time refactoring it. So if we want to return a Widget,then a simple Widget return is the best way to go.</p><p><em>//BAD EXAMPLE</em></p><pre>CustomWidget returnCustomWidget() {<br>  return CustomWidget();<br>}</pre><p><em>//GOOD EXAMPLE</em></p><pre>Widget returnCustomWidget() {<br>  return CustomWidget();<br>}</pre><h3>21. Don’t use Containers for everything</h3><p>Containers are great, but they are still heavy Widgets.</p><p>For example, if you want to add just Size to you Widget, a simple <strong>SizedBox</strong> performs better, than a whole Container.</p><p>If you want just padding, use <strong>Padding</strong>.</p><p>Another example, if you want your Widget to be rounded, or add a color to it, or some custom decoration, <strong>DecoratedBox</strong> is the way to go.</p><p>If you need Constraints, instead of a Container, use <strong>ConstrainedBox</strong>.</p><p>If you just want to align your content, use the <strong>Align </strong>Widget.</p><p>But if you need more of these, a <strong>Container</strong> is more readable, than nesting all of these together.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d72680986e24" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>