Benefits of using Dart (and Flutter)
Tags: dart, flutter
Published: 2020-02-17 18:54:55

Programmers don’t like to deal with stuff that is not directly related to their actual problems that they’re trying to solve. So the more comfortable it is to use a programming language, the easier it is to focus on the substantive difficulties.

The following list shows different ways that support you in writing maintainable code quickly when using Dart.

![© commons.wikimedia.org](https://cdn-images-1.medium.com/max/878/1*EGd22O-yJPVH3cmTtfmWMQ.png)

---

# Extensions

When working with third-party libraries, you often wish that a certain functionality exists. In most cases, it’s tightly coupled to the current project and wouldn’t qualify as a pull-request to the library’s repository. In Dart, it is possible to extend third-party libraries.

{% gist https://gist.github.com/Chroma91/1815e1d16a2c23efad3baf3ec2a3a6e3 file=extension_3rd_party.dart %}

Furthermore, it’s even possible to extend native types like **String** or **List**.

{% gist https://gist.github.com/Chroma91/1815e1d16a2c23efad3baf3ec2a3a6e3 file=extension_native_type_string.dart %}

You can also utilize generics. Check out the code snippet below:

{% gist https://gist.github.com/Chroma91/1815e1d16a2c23efad3baf3ec2a3a6e3 file=extension_native_type_list.dart %}

By extending the capabilities of the language, it is also more comfortable for your colleagues to find the new functionalities you implemented. They don’t have to check some directory you probably named ‘util’ or in a similar fashion. When working with the code, they’ll explore all the new bits naturally through their IDE (auto-complete / auto-suggest).

---

# Named Parameters

Don’t we all always have to look up parameters for specific functions or methods? When using Dart, you can define named parameters. These are a convenient way to make your API more natural to use. However, it can also encourage lousy design decisions by overloading functions with a ton of parameters. Just beware not to go overboard with it. Just use it as a tool to make your code more readable and easier to use!

If you want to declare a parameter as necessary for the execution of the function, you can add an annotation (see _@required_ in the code example below).

{% gist https://gist.github.com/Chroma91/1815e1d16a2c23efad3baf3ec2a3a6e3 file=named_parameters.dart %}

---

# Named Constructors

Named constructors can come in handy when an object typically gets constructed with the same parameters in multiple places. Or maybe you want to make it more clear what exactly you’re initializing.

{% gist https://gist.github.com/Chroma91/1815e1d16a2c23efad3baf3ec2a3a6e3 file=named_constructors.dart %}

---

# LINQ-like Collection Methods

> _‘Language-Integrated Query (LINQ) is the name for a set of technologies based on the integration of query capabilities directly into the C# language.’_

> — Microsoft C# documentation

Take a look at the following example:

{% gist https://gist.github.com/Chroma91/1815e1d16a2c23efad3baf3ec2a3a6e3 file=linq.dart %}

It feels natural, and you instantly know what the code achieves here. There are a lot more convenient methods that you’ll automatically find when writing your code. Yet, you might notice the ‘.toList()’ method. That is required because Iterables are lazy in Dart and we have to convert it to a list.

---

# Operator Overloading

Merging multiple objects is a common task in web programming. Make your life easier and overload the operators! You decide what happens when someone tries to interact with your objects. This way, your colleagues can use your objects within the natural constraints of the language. They don’t need to worry about deep-merging or other issues.

{% gist https://gist.github.com/Chroma91/1815e1d16a2c23efad3baf3ec2a3a6e3 file=operator_overloading.dart %}

---

# Streams / StreamBuilder

Handling with asynchronicity is always a source of frustration. Instead of only knowing when a promise got resolved or rejected, we also get the current progress of the function with streams. Writing good user interfaces is extremely straightforward because you can always communicate the current state to the user. With streams, you get constant feedback for free.

{% gist https://gist.github.com/Chroma91/1815e1d16a2c23efad3baf3ec2a3a6e3 file=stream_builder.dart %}

The StreamBuilder is a Flutter widget that makes dealing with streams smooth. If you’re only interested in the outcome of an asynchronous function, you can stick to Futures, which work precisely like Promises in JavaScript.

---

# Optimizations included

In web apps, you usually reach for Webpack or similar tools to bundle your codebase for production. The setup can become very cumbersome and hard sometimes. Dart includes optimizations like tree-shaking out-of-the-box. You don’t need to configure anything as opposed to all the other tools that claim that they are ‘zero-config’.

---

# Compile it to ANYTHING

With the help of Flutter, it’s possible to compile your codebase to work with various platforms (Android, iOS, Web, macOS, Linux, Windows). However, the native implementations (macOS, Linux, Windows) are still in alpha or technical preview status.

The Flutter team has implemented all of the native control elements from scratch. That means that you can write an app that looks the same on iOS and Android. So you can use Material Design widgets for iOS and vice-versa. Of course, it’s also possible to have distinct widgets for each platform. It is an excellent example of WYSIWYG because of the custom implementation of the renderer (Skia).

---

# Insanely fast Hot-Reload

You’ll love Flutter’s hot-reload feature right away. This mechanic exists in other frameworks like React, too. Nevertheless, they currently can’t compete with the one in Flutter. E.g., if you change a color, the change is displayed immediately. There’s not even a sign of a hot-reload taking place. It is a feature that you have to see with your own eyes.

---

# Conclusion

With Dart, it’s possible to keep a fast pace while developing your app and still produce maintainable code. All the listed benefits automatically decrease the barrier for your colleagues to understand your intentions. If you’re currently using TypeScript or a comparable language, it’s worth a try to implement Dart in one of your new projects. You’ll feel an improvement even if only a subset of the advantages mentioned in this article concerns you.