Dart first impressions

I’m taking a introductory mobile development class this term, which is mainly taught in Flutter/Dart, so I’m in the initial stages of learning the Dart programming language at the moment. I enjoy learning new programming languages, though that’s not to say there aren’t some pain points at times (looking at you, JavaScript).

In practice, Dart is pretty tied up with Flutter, as it seems the main paradigm in which it’s used is in combination with Flutter for mobile development. That said, I won’t be talking a lot about Widgets and such, as I haven’t gotten too deep into the Flutter side of things yet. But, here’s a few initial impressions of the Dart programming language.

The language definitely feels modern, with lots of syntactic sugar and powerful methods/functions built in. It’s much more like programming in Python than C++, for instance. Just look at this gorgeously simple class constructor.

 Planet({required this.name, required this.description});

That’s it. The brackets signify that the contents are keyword arguments, and the this.name syntax assigns the argument to the corresponding class attribute automatically. Lovely! The required, of course, marks these as non-optional.

Dart is strongly typed, which I like. However, it can also infer types, so something like this is fine.

var x = 21;

It also has enough flexibility to allow non-specific types, as in code like the following.

dynamic getRandomListElement(List<dynamic> list) {
  if (list.isNotEmpty) {
    return list[Random().nextInt(list.length)];
  } else {
    return null;
  } 
}

This will work equally well on a list<int> or list<String>. Cool stuff.

Now, let’s talk about null safety. Dart is not messing around when it comes to null safety. I’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.

  Planet getNextLegDestination() {

    String answer = '';
    Planet planet;

    while (answer != 'Y' && answer != 'N') {
      answer = promptGetResponse(
        'Shall I randomly choose a planet for you to visit? (Y or N)'
      );
      if (answer == 'Y') {
        planet = hostSystem.getRandomPlanet();
      } else if (answer == 'N') {
        planet = hostSystem.getNamedPlanet();
      } else {
        print('Sorry, I didn\'t get that.');
      }
    }
    return planet;
  }

It’s convinced that the return planet; at the end is not null safe, even though the while loop can never cease without assigning a value to planet. My workaround was to do this instead, which required adding a default null planet attribute to another class.

Planet planet = hostSystem.invalidPlanet;
// Planet planet;

Another solution would be to mark variables as nullable (e.g., Planet? planet;), 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’t built to have a ‘blank’ 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, “With null safety, your runtime null-dereference errors turn into edit-time analysis errors.”, which is definitely a good reason to get used to dealing with this.

For a few final thoughts, the official documentation is pretty good, but certainly not the best I’ve used. I also find it interesting that pretty much everything is an object — even int. I’m not sure how to feel about this!

Overall, I’m finding Dart easy to learn and use, though I don’t really have any information at this point about how fast it runs, or deeper details about its implementation. Grade so far: B+.

Song of the week: In the Shade of the Sun — Kapitan Korsakov

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *