Posts

Showing posts with the label dart

New Dart SDK helps eliminates symlinks

Image
In which we retell the story of Dart and symlinks, investigate the new .packages file, and create a simpler world for Dart developers everywhere. tldr: The Dart team is working towards a world where symlinks are no longer required because of the new .packages file. Why a new solution for locating packages? In the long long ago, during the before times (aka 2011), Dart could only run monolithic scripts and apps. In those early days, Dart didn't have support for packages/shared libraries. Then came package: URIs, which opened the door for sharing code. However, the platform only had one way to resolve that package: URI: look inside a packages/ directory next to the file importing the library. (Later, a --package-root option was added for more flexibility.) Thus began Dart's reliance on symlinks  as a way of creating the packages/ directories next to all locations containing Dart scripts or libraries. Those symlinks and the package: scheme helped ushered in a boom of t...

Null-aware operators in Dart

Image
Three new language features just landed in the latest dev channel build of the Dart ! Collectively known as null-aware operators , these new features will help you reduce the code required to work with potentially null objects. I'm excited for these new abilities, because typing less is always a good thing. Read on to learn more, and be sure to try these new features on Dart Pad . ?? Use ?? when you want to evaluate and return an expression IFF another expression resolves to null. exp ?? otherExp is similar to ((x) => x == null ? otherExp : x)(exp) ??= Use ??= when you want to assign a value to an object IFF that object is null. Otherwise, return the object. obj ??= value is similar to ((x) => x == null ? obj = value : x)(obj) ?. Use ?. when you want to call a method/getter on an object IFF that object is not null (otherwise, return null). obj?.method() is similar to ((x) => x == null ? null : x.method())(obj) Yo...

Formatting Dart code before every git commit

Dart 's dartfmt tool is a really neat utility to automatically format your code. Use the dartfmt tool in your workflow to ensure your code complies with the Dart style guide . Of course, you don't want to manually run dartfmt. Instead, you want to automate it. Use git's pre-commit hook to ensure your code is formatted, before it is committed. Add the following code to your . git/hooks/pre-commit script for your local repo, and make sure the script is executable. #!/bin/bash DARTFMT_OUTPUT=`dartfmt -w . | grep Formatted` if [ -n "$DARTFMT_OUTPUT" ]; then   echo $DARTFMT_OUTPUT   echo "Re-attempt commit."   exit 1 else   echo "All Dart files formatted correctly. Yay!"   exit 0 fi If your code needs formatting, it will be formatted and written to disk. The commit will fail, so you have a chance to inspect the changes. You can enforce formatting with your Continuous Integration system. Try these instructions ...

I ported a JavaScript app to Dart. Here's what I learned.

Image
In which I port a snazzy little JavaScript audio web app to Dart , discover a bug, and high-five type annotations. Here's what I learned. [As it says in the header of this blog, I'm a seasoned Dart developer. However, I certainly don't write Dart every day (I wish!). Don't interpret this post as "Hi, I'm new to Dart". Instead, interpret this post as "I'm applying what I've been documenting."] This post analyzes two versions of the same app, both the original (JavaScript) version and the Dart version. The original version is a proxy for any small JavaScript app, there's nothing particularly special about the original version, which is why it made for a good example. This post discusses the differences between the two implementations: file organization, dependencies and modules, shims, classes, type annotations, event handling, calling multiple methods, asynchronous programming, animation, and interop with JavaScript libraries. F...