<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
        <title>Dart - series - Notes about stuff</title>
        <link>/series/dart/</link>
        <description>Dart - series - Notes about stuff</description>
        <generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Mon, 09 Jan 2023 05:47:54 &#43;0100</lastBuildDate><atom:link href="/series/dart/" rel="self" type="application/rss+xml" /><item>
    <title>Dart for Java developers</title>
    <link>/posts/2023-01-09-dart/</link>
    <pubDate>Mon, 09 Jan 2023 05:47:54 &#43;0100</pubDate>
    <author>Kamila Chyla</author>
    <guid>/posts/2023-01-09-dart/</guid>
    <description><![CDATA[<div class="featured-image">
                <img src="/dart.png" referrerpolicy="no-referrer">
            </div><p>Welcome in a new series of articles about Dart programming language.</p>
<p>It is not a beginner tutorial. I focus mainly on features that may be surprising and/or interesting for a Java(Script) developer.</p>
<h1 id="slides">Slides</h1>
<p>Slide notes are available <a href="/dart_for_java_devs.pdf" rel="">here</a>.</p>
<h1 id="the-resources">The resources:</h1>
<p>Start with these:&hellip;</p>
<ol>
<li><a href="https://dart.dev/guides/language/language-tour" target="_blank" rel="noopener noreffer ">https://dart.dev/guides/language/language-tour</a>  (language overview)</li>
<li><a href="https://dartpad.dev/" target="_blank" rel="noopener noreffer ">https://dartpad.dev/</a> (write &amp; run in online editor)</li>
<li><a href="https://dart.dev/guides/language/spec" target="_blank" rel="noopener noreffer ">https://dart.dev/guides/language/spec</a> (language specification page, <a href="https://dart.dev/guides/language/specifications/DartLangSpec-v2.10.pdf" target="_blank" rel="noopener noreffer ">current spec pdf: 2.10</a>)</li>
<li><a href="https://dart.dev/guides/language/effective-dart" target="_blank" rel="noopener noreffer ">https://dart.dev/guides/language/effective-dart</a> (good practices)</li>
</ol>
<h2 id="basics">Basics</h2>
<p>&hellip; or look below:</p>
<ol>
<li><code>.dart</code> file is a &ldquo;module&rdquo;.</li>
<li>top-level functions, variables, constants and classes</li>
<li>names starting with &ldquo;_&rdquo; (underscore) are private to the module</li>
<li>everything is an object</li>
<li>null-safety (nullable type by appending <code>?</code> to type name, like in Kotlin)</li>
<li>generics are are reified (accessible at runtime), contrary to Java, where types are erased.</li>
<li>type inference; you can declare variables with <code>var</code>.</li>
<li><code>final</code> modifier makes variable assign-once; <code>const</code> modifier makes variable a compile-time constant.</li>
<li>import from built-in libraries (<code>import 'dart:html';</code>), local filesystem (<code>import './foo.dart</code>) or external package managed by build tool, like <code>pub</code> (<code>import 'package:test/test.dart';</code>)</li>
<li>you can alias a lib, import only one symbol or exclude symbol from import (<code>as</code>, <code>show</code>, <code>hide</code>)</li>
<li>asynchrony support with <code>async</code> and <code>await</code>, <a href="https://api.dart.dev/stable/dart-async/Future-class.html" target="_blank" rel="noopener noreffer ">Future</a> and <a href="https://api.dart.dev/stable/dart-async/Stream-class.html" target="_blank" rel="noopener noreffer ">Stream</a> classes, which are run in same <a href="https://api.dart.dev/stable/dart-isolate" target="_blank" rel="noopener noreffer ">Isolate</a> - see [Concurrency in Dart)[https://api.dart.dev/stable/dart-isolate)</li>
<li>sync and async generator functions</li>
<li>callable classes: instances can be called with params; implement <code>call</code> method in the class</li>
<li><em>isolates</em> as &ldquo;threads&rdquo; without race conditions, see <a href="https://dart.dev/guides/language/concurrency" target="_blank" rel="noopener noreffer ">concurrency</a></li>
<li>typedefs (<code>typedef IntList = List&lt;int&gt;;</code>_), also generic, and (recommended) function types, eg: <code>final bool Function(Event) _predicate</code>  or <code>final List&lt;void Function(Event)&gt; _observers;</code></li>
<li>Control structures: <code>for</code> (with iterator in form <code>for elem of iter</code>), <code>while(){}</code>, <code>do{} while()</code>, <code>switch</code> (without pattern matching)</li>
<li><a href="https://dart.dev/guides/language/extension-methods" target="_blank" rel="noopener noreffer ">Extension methods</a> add functionality to existing classes. Resolve to a static type of the receiver (like in Kotlin).</li>
</ol>
<h2 id="classes">Classes</h2>
<p>As Java developers we usually think in terms of classes. Dart supports classs, allows to define abstract classes and offers mixins (that define behaviors and don&rsquo;t have constructors) with which a class can be extended.</p>
<p>See <a href="https://dart.dev/guides/language/language-tour#classes" target="_blank" rel="noopener noreffer ">classes section in lnguage tour</a>.</p>
<p>If, by any  chance, you are a JavaScript developer, you might want to read a decicated  <a href="https://dart.dev/guides/language/coming-from/js-to-dart" target="_blank" rel="noopener noreffer ">guide for JavaScript prorgammers</a> with examples.</p>
<h2 id="libraries">Libraries</h2>
<p>If you want to dive deep into libraries, start with standard library. Then, depending on the target platform, look at native or javacript secions below.</p>
<h3 id="standard-library">Standard library</h3>
<p>These libraries workk on all platforms:</p>
<ul>
<li><a href="https://api.dart.dev/stable/dart-core/dart-core-library.html" target="_blank" rel="noopener noreffer ">dart:core</a></li>
<li><a href="https://api.dart.dev/stable/dart-async/dart-async-library.html" target="_blank" rel="noopener noreffer ">dart:async</a> and <a href="https://pub.dev/packages/async" target="_blank" rel="noopener noreffer ">package:async</a></li>
<li><a href="https://api.dart.dev/stable/dart-collection/dart-collection-library.html" target="_blank" rel="noopener noreffer ">dart:collection</a> and <a href="https://pub.dev/packages/collection" target="_blank" rel="noopener noreffer ">package:collection</a></li>
<li><a href="https://api.dart.dev/stable/dart-convert/dart-convert-library.html" target="_blank" rel="noopener noreffer ">dart:convert</a> and <a href="https://pub.dev/packages/convert" target="_blank" rel="noopener noreffer ">package:convert</a></li>
<li><a href="https://api.dart.dev/stable/dart-developer/dart-developer-library.html" target="_blank" rel="noopener noreffer ">dart:developer</a> (only in <a href="https://dart.dev/overview#native-platform" target="_blank" rel="noopener noreffer ">Native JIT</a> and <a href="https://dart.dev/tools/dartdevc" target="_blank" rel="noopener noreffer ">dartdevc</a>)</li>
<li><a href="https://api.dart.dev/stable/dart-math/dart-math-library.html" target="_blank" rel="noopener noreffer ">dart:math</a></li>
<li><a href="https://api.dart.dev/stable/dart-typed_data/dart-typed_data-library.html" target="_blank" rel="noopener noreffer ">dart:typed_data</a> (and also <a href="https://pub.dev/packages/typed_data" target="_blank" rel="noopener noreffer ">package:typed_data</a>)</li>
</ul>
<h3 id="dart-native">Dart native</h3>
<p><a href="https://dart.dev/overview#native-platform" target="_blank" rel="noopener noreffer ">Dart native</a> platform allows to create AOT- and JIT-compiled code and provides a few special libraries:</p>
<ul>
<li><a href="https://api.dart.dev/stable/dart-ffi/dart-ffi-library.html" target="_blank" rel="noopener noreffer ">dart:ffi</a> and  <a href="https://pub.dev/packages/ffi" target="_blank" rel="noopener noreffer ">package:ffi</a></li>
<li><a href="https://api.dart.dev/stable/dart-io/dart-io-library.html" target="_blank" rel="noopener noreffer ">dart:io</a> and <a href="https://pub.dev/packages/io" target="_blank" rel="noopener noreffer ">package:io</a></li>
<li><a href="https://api.dart.dev/stable/dart-isolate/dart-isolate-library.html" target="_blank" rel="noopener noreffer ">dart:isolate</a></li>
<li><a href="https://api.dart.dev/stable/dart-mirrors/dart-mirrors-library.html" target="_blank" rel="noopener noreffer ">dart:mirrors</a></li>
</ul>
<h3 id="javascript">Javascript</h3>
<p><a href="https://dart.dev/overview#web-platform" target="_blank" rel="noopener noreffer ">Dart Web platform</a> allows developing for web.</p>
<ul>
<li><a href="https://api.dart.dev/stable/dart-html/dart-html-library.html" target="_blank" rel="noopener noreffer ">dart:html</a></li>
<li><a href="https://api.dart.dev/stable/dart-indexed_db/dart-indexed_db-library.html" target="_blank" rel="noopener noreffer ">dart:indexed_db</a></li>
<li><a href="https://api.dart.dev/stable/dart-js/dart-js-library.html" target="_blank" rel="noopener noreffer ">dart:js</a></li>
<li><a href="https://api.dart.dev/stable/dart-js_util/dart-js_util-library.html" target="_blank" rel="noopener noreffer ">dart:js_util</a></li>
<li><a href="https://pub.dev/packages/js" target="_blank" rel="noopener noreffer ">package:js</a> - also  <a href="https://dart.dev/web/js-interop" target="_blank" rel="noopener noreffer ">JavaScript interoperability</a></li>
<li><a href="https://api.dart.dev/stable/dart-svg/dart-svg-library.html" target="_blank" rel="noopener noreffer ">dart:svg</a></li>
<li><a href="https://api.dart.dev/stable/dart-web_audio/dart-web_audio-library.html" target="_blank" rel="noopener noreffer ">dart:web_audio</a></li>
<li><a href="https://api.dart.dev/stable/dart-web_gl/dart-web_gl-library.html" target="_blank" rel="noopener noreffer ">dart:web_gl</a></li>
</ul>
<h2 id="nice-cookbook-articles">Nice cookbook articles:</h2>
<p>The <a href="https://docs.flutter.dev/cookbook" target="_blank" rel="noopener noreffer ">cookbook</a> is a nice collection of hints on how to implement specific Dart/Flutter usecases, for example: <a href="https://docs.flutter.dev/cookbook/networking/background-parsing#4-move-this-work-to-a-separate-isolate" target="_blank" rel="noopener noreffer ">parsing json in backgroud</a>, <a href="https://docs.flutter.dev/cookbook/navigation/returning-data" target="_blank" rel="noopener noreffer ">return data from screen</a>, <a href="https://docs.flutter.dev/cookbook/networking/authenticated-requests" target="_blank" rel="noopener noreffer ">make authenticated requests</a> or <a href="https://docs.flutter.dev/cookbook/persistence/sqlite" target="_blank" rel="noopener noreffer ">persist data with sqlite</a>, <a href="https://docs.flutter.dev/cookbook/testing/integration/introduction" target="_blank" rel="noopener noreffer ">how to write integration tests</a>&hellip;</p>
<h1 id="next-steps">Next steps</h1>
<p>As a next step, we may want to write some short prorgams just to check out some ideas and get used to the language itself. Stay tuned!</p>
<h1 id="happy-coding">Happy coding!</h1>
]]></description>
</item>
</channel>
</rss>
