From Java to Dart

You may be an Android or Java developer, you’ve heard about Flutter, and are curious. So, is Dart easy to pick up as a Java developer?

The official website claims that Dart is “familiar to many existing developers”, and I generally agree with this statement. There is now a codelab Intro to Dart for Java developers: I do recommend you at least skim through it to get an overview.

In addition to the codelab, below is a list of concepts you will probably need to know fairly quickly.

No arrays, just lists

In Java, we are used to Lists and Arrays being 2 different things. But, in Dart, everything is a List. Therefore, only one API to learn, and it is fairly similar to the Java List API.

Data type for image

One of the most commonly used array in an Android app is byte[] , used for an image. So, what is the type for image raw data in Dart? It’s an Uint8List. It has one quirk though. You need to  import 'dart:typed_data'; .

Maps API is a bit weird

Mostly, the Map API is what you would expect and the notation is similar to arrays for accessing and changing values.

However, one method may confuse you. I’m referring to  V putIfAbsent(K key, V ifAbsent()). As the doc says, it “Look up the value of key, or add a new value if it isn’t there “.  So it’s not the same as the “put” method you are used to in Java! Additionally, if you look at the method signature, the second parameter is a function.

Below is an example showing the basic ways to use a Map.

Null is a type

When an object is null, it is actually an instance of Null. Therefore, it is safe to call toString()  on it (eg for logging purposes).

 

Implement a singleton pattern

Seth Ladd has provided an answer to this on Stack Overflow, so I’ll copy it here (converted to Dart 2), and explain it.

 

In Dart, methods do not use private/public identifiers, but rather private methods are preceded with a dash. So _internal()  is simply a method called “internal” that is private to the class Singleton.

Invoking the static private instance _singleton  is done via a constructor. This means that each time you use the same static instance, it appears that you may be creating a new instance, even though you are not. Dart 2 makes this slightly less confusing, as you don’t need the “new” keyword to invoke the constructor, so your brain doesn’t keep thinking “but I don’t want a new instance!”. If this bothers you, you could make the instance public, by removing the dash in front of its name.

Future API

Async programming is very important to mobile apps, and this is done with Futures in Dart.  There are 2 ways to use Futures in Dart: with the Future API, or with async/await.

I find async/await to be very powerful and super easy to understand. In particular, errors are dealt with in a manner understood easily coming from a Java background, that is the try/catch block.

For a specific Flutter example, you can check out my How to write async code in Flutter code tutorial.

 

[donationarticle]

What next?

In early 2018, Dart was updated to v2 and Flutter is now fully compliant. This, actually, has proved to be a little bit of a hurdle, because not all doc has been updated  yet (eg Effective Dart) and most of the Stack Overflow answers predates Dart 2. Having said that, Dart 2 is firmly establishing itself in the community, so I don’t think it will be much of a problem by the end of the year.

Author: Natalie Masse Hooper

Mobile app developer with 14 years experience. Started with the Android SDK in 2010 and switched to Flutter in 2017. Past apps range from start ups to large tech (Google) and non tech (British Airways) companies, in various sectors (transport, commercial operations, e-commerce).

One thought on “From Java to Dart”

  1. Useful highlights, even for a non Java developer.

    What path (resources) would your recommend for a good start developing with Flutter, considering best practices towards architecture and performance?

    Your posts touch important points, btw.

    Kudos!

Leave a Reply

Your email address will not be published.