{"id":14,"date":"2022-01-17T19:14:23","date_gmt":"2022-01-17T19:14:23","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/techgooder\/?p=14"},"modified":"2022-01-17T19:14:23","modified_gmt":"2022-01-17T19:14:23","slug":"dart-first-impressions","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/techgooder\/2022\/01\/17\/dart-first-impressions\/","title":{"rendered":"Dart first impressions"},"content":{"rendered":"\n<p>I&#8217;m taking a introductory mobile development class this term, which is mainly taught in Flutter\/Dart, so I&#8217;m in the initial stages of learning the Dart programming language at the moment. I enjoy learning new programming languages, though that&#8217;s not to say there aren&#8217;t some pain points at times (looking at you, JavaScript).<\/p>\n\n\n\n<p>In practice, Dart is pretty tied up with Flutter, as it seems the main paradigm in which it&#8217;s used is in combination with Flutter for mobile development. That said, I won&#8217;t be talking a lot about Widgets and such, as I haven&#8217;t gotten too deep into the Flutter side of things yet. But, here&#8217;s a few initial impressions of the Dart programming language.<\/p>\n\n\n\n<p>The language definitely feels modern, with lots of syntactic sugar and powerful methods\/functions built in. It&#8217;s much more like programming in Python than C++, for instance. Just look at this gorgeously simple class constructor.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> Planet({required this.name, required this.description});<\/code><\/pre>\n\n\n\n<p>That&#8217;s it. The brackets signify that the contents are keyword arguments, and the <code>this.name<\/code> syntax assigns the argument to the corresponding class attribute automatically. Lovely! The <code>required<\/code>, of course, marks these as non-optional.<\/p>\n\n\n\n<p>Dart is strongly typed, which I like. However, it can also infer types, so something like this is fine.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var x = 21;<\/code><\/pre>\n\n\n\n<p>It also has enough flexibility to allow non-specific types, as in code like the following.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dynamic getRandomListElement(List&lt;dynamic&gt; list) {\r\n  if (list.isNotEmpty) {\r\n    return list&#091;Random().nextInt(list.length)];\r\n  } else {\r\n    return null;\r\n  } \r\n}<\/code><\/pre>\n\n\n\n<p>This will work equally well on a <code>list&lt;int&gt;<\/code> or <code>list&lt;String&gt;<\/code>. Cool stuff.<\/p>\n\n\n\n<p>Now, let&#8217;s talk about <a href=\"https:\/\/dart.dev\/null-safety\" data-type=\"URL\" data-id=\"https:\/\/dart.dev\/null-safety\">null safety<\/a>. Dart is not messing around when it comes to null safety. I&#8217;m onboard with the basic idea of it, but it has caused some annoyances so far. For instance, the compiler will complain about the following code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  Planet getNextLegDestination() {\r\n\r\n    String answer = '';\r\n    Planet planet;\r\n\r\n    while (answer != 'Y' &amp;&amp; answer != 'N') {\r\n      answer = promptGetResponse(\r\n        'Shall I randomly choose a planet for you to visit? (Y or N)'\r\n      );\r\n      if (answer == 'Y') {\r\n        planet = hostSystem.getRandomPlanet();\r\n      } else if (answer == 'N') {\r\n        planet = hostSystem.getNamedPlanet();\r\n      } else {\r\n        print('Sorry, I didn\\'t get that.');\r\n      }\r\n    }\r\n    return planet;\r\n  }<\/code><\/pre>\n\n\n\n<p>It&#8217;s convinced that the <code>return planet;<\/code> at the end is not null safe, even though the while loop can never cease without assigning a value to <code>planet<\/code>. My workaround was to do this instead, which required adding a default null planet attribute to another class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Planet planet = hostSystem.invalidPlanet;\r\n\/\/ Planet planet;<\/code><\/pre>\n\n\n\n<p>Another solution would be to mark variables as nullable (e.g., <code>Planet? planet;<\/code>), but this requires adapting variables types and return types all over the place. Having to have default, non-null assignments definitely seems like it will be problematic at times, especially when you have a complex class that maybe isn&#8217;t built to have a &#8216;blank&#8217; instantiation. The whole thing ends up a bit awkward for me, but maybe I just need more time to get used to it. As the documentation states, &#8220;With null safety, your\u00a0<strong>runtime<\/strong>\u00a0null-dereference errors turn into\u00a0<strong>edit-time<\/strong>\u00a0analysis errors.&#8221;, which is definitely a good reason to get used to dealing with this.<\/p>\n\n\n\n<p>For a few final thoughts, the official documentation is pretty good, but certainly not the best I&#8217;ve used. I also find it interesting that pretty much everything is an object &#8212; even <code>int<\/code>. I&#8217;m not sure how to feel about this!<\/p>\n\n\n\n<p>Overall, I&#8217;m finding Dart easy to learn and use, though I don&#8217;t really have any information at this point about how fast it runs, or deeper details about its implementation. Grade so far: <strong>B+<\/strong>.<\/p>\n\n\n\n<p>Song of the week: <a href=\"https:\/\/www.youtube.com\/watch?v=TfK-Q-KdrVE\" data-type=\"URL\" data-id=\"https:\/\/www.youtube.com\/watch?v=TfK-Q-KdrVE\">In the Shade of the Sun &#8212; Kapitan Korsakov<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m taking a introductory mobile development class this term, which is mainly taught in Flutter\/Dart, so I&#8217;m in the initial stages of learning the Dart programming language at the moment. I enjoy learning new programming languages, though that&#8217;s not to say there aren&#8217;t some pain points at times (looking at you, JavaScript). In practice, Dart &hellip; <a href=\"https:\/\/blogs.oregonstate.edu\/techgooder\/2022\/01\/17\/dart-first-impressions\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Dart first impressions&#8221;<\/span><\/a><\/p>\n","protected":false},"author":12022,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-14","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/techgooder\/wp-json\/wp\/v2\/posts\/14","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/techgooder\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/techgooder\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/techgooder\/wp-json\/wp\/v2\/users\/12022"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/techgooder\/wp-json\/wp\/v2\/comments?post=14"}],"version-history":[{"count":1,"href":"https:\/\/blogs.oregonstate.edu\/techgooder\/wp-json\/wp\/v2\/posts\/14\/revisions"}],"predecessor-version":[{"id":15,"href":"https:\/\/blogs.oregonstate.edu\/techgooder\/wp-json\/wp\/v2\/posts\/14\/revisions\/15"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/techgooder\/wp-json\/wp\/v2\/media?parent=14"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/techgooder\/wp-json\/wp\/v2\/categories?post=14"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/techgooder\/wp-json\/wp\/v2\/tags?post=14"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}