Dart Tutorials

  1. Introduction
    1. Focus Points
  2. Part 1: Variables and Types
  3. Part 2: Functions
  4. Part 3: Classes
    1. Null-safety in Dart
    2. What do I do about null-safety?

Introduction

Prior labs linked many Dart-related resources to you. This lab will have you following videos that introduce critical concepts in Dart, including features of the language that you may not have encountered in other languages.

You will not be using VSCode or Flutter in this lab. You be using a browser-based programming environment called Dartpag. Everything you learn lessons will be applied in this course to VSCode and Flutter.

It is essential that program along with the videos using Dartpad. Don’t just watch!

These are, of course, not my videos. The creator does a good job, and all of his other videos are excellent as well if you are looking for more instructional supplements.

Focus Points

All content in these tutorials is important, but pay special attention to the topics below. You will be using them frequently when creating Flutter apps.

Part 1: Variables and Types

Open Dartpad to code. Watch and follow along with Part 1: Variables and Types.

Study Questions:

Part 2: Functions

Watch and follow along with Part 2: Functions.

Study Questions:

Part 3: Classes

Watch and follow along with Part 3: Classes. Stop after Video 20 on “Overriding the toString Method”. The remainder of the sections on abstract classes aren’t necessary.

Null-safety in Dart

While following along with the video, you will get warnings from Dartpad, but the video creator doesn’t have this issue. What’s going on?

Dartpad null safety warning

What’s going on is null-safety. This concept was introduced in Dart 2.0, which came around after the tutorial was recorded.

With null-safety, you must explicitly indicate if you want a variable to be capable of having a null value. You indicate you want a variable capable being null by declaring its type as, for example, String? name vs. String name. The ? indicates nullability.

If a variable is not nullable, then you must initialize it with a value, for example. String s = 'Bob'; is allowed, but String s; is not. This applies also to class variables and constructors.

Null-safety warnings in the Person class

In the video, the author creates a Person class with variables that are declared as non-null, but his constructor uses optional parameters with no default values. This means that the class variables might not be initialized when the constructor is called, i.e., may be null. Dart 2.0 won’t allow the name, age, and height variables to possibly be null here, but older versions of Dart (like is used in the video) will allow it.

What do I do about null-safety?

So what is the fix? The right answer depends on the design and goals of your program. The first question to answer is, “Does it make sense for this varable to have a null value?” I think in the case of the Person class, the answer is “no”. Why not? Because a Person should always have a name, age, and height logically and abstractly speaking. That’s probably true in your code as well.

But there are many scenarios where a variable should be nullable. The most common is when you have a variable representing a value in some class instances but not all. For example, a Person may not have a String middleName or a double salary. In this case, it makes sense to mark such variables as nullable. Another scenario is when you know you will need a value, but that value will be loaded asynchronously, e.g., a call to an Internet database to download a list of friends that may take some time to complete. We will encounter this scenario later in the semester.

Okay, so you ask the question, “Should this variable be null”?

But what about class variables and constructors? You have a few options.

Option 1 is to change the named parameters to positional parameters. Positional parameters are treated in order, like in Java or Python. Positional parameters are always required. Note there are no squiggly braces {} in the constructor:

Person class with positional parameters in constructor

Option 2 will keep the named parameters but mark them as required. This will force the code that calls the constructor to provide argument values for all parameters:

Person class with required named parameters in constructor

Option 3 will keep the named parameters but provide them with a non-null default value. This is a great option when it makes logical sense to have a default value, say when you are drawing colored circles and you want them to be blue by default but give the coder the option of specifying another color. In the case of a Person class, providing default values for name, age, and height doesn’t make sense.

Person class with required named parameters in constructor

The Solution: So what should you do for the scenario in this tutorial? Flutter’s convention is to use named parameters where possible for clarity and maintainability. So, in this case, I recommend you choose Option 2.

Study Questions: