Dart Tutorials
- Introduction
- Part 1: Variables and Types
- Part 2: Functions
- Part 3: Classes
- Additional resources: the Dart language tour
- Study Questions
- Homework Assignment 1
Introduction
Dart is an object-oriented programming language with a syntax similar to Python and Java. This lab will get you started on the basic elements of the Dart language so that you can begin to build Flutter applications.
You will not be using Visual Studio Code or Flutter in this lab. You be using a browser-based programming environment called Dartpad. 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 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.
- string interpolation
- The
=>
keyword - Optional parameters
- Named parameters
- Class constructors
Part 1: Variables and Types
Open Dartpad to code. Watch and follow along with Part 1: Variables and Types.
Study Questions:
- What is “string interpolation”?
- What is “type inference”? Which Dart keyword indicates that a variable’s type should be inferred?
- what is the purpose of the
final
keyword - Andrea asserts that “it is better to declare variables as
final
when you can.” Why do you think that is? There are at least two good reasons. hint: it has nothing to do with type safety.
Part 2: Functions
Watch and follow along with Part 2: Functions.
Study Questions:
- When you define a function, you define its name and parameters. Parameters are variables. When you call a function, you pass along arguments – these are the values that function parameters take. Andrea mistakenly uses the term argument instead of parameter when defining a function. In the video, he says they mean the same thing, but they don’t. The distinction may seem minor, but you should use proper terms.
- Parameters:
void describe(String name, int age, double height) {}
- Arguments:
describe('Alice', 21, 63);
- Parameters:
- How do you indicate that a function parameter is optional?
- how do you indicate that a function parameter is optional and named?
- What is a default parameter value, and how do you specify it?
- What is the purpose of the
=>
operator? What is its main limitation?
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?
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.
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”?
- If the answer is “yes”, simply change the declaration from, e.g.,
String name;
toString? name;
. The?
indicates the variable can be either a String or null. - If the answer is “no”, simply intialize the variable with a value, e.g.,
String name = 'Bob';
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:
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:
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.
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.
Additional resources: the Dart language tour
- Read through the official documentation page A tour of the Dart language. This is a huge page, and you are not expected to remember everything.
- Look for familiar tasks from your previous languages, like how to define variables, the structure of basic expressions, how to define methods, etc.
- Take note of the new or unusual things.
- The Dart cheatsheet is another useful resource.
Bookmark these pages in your web browser. Come back to them when you think “How do you do X in Dart?”
Study Questions
- What is null-safety? Why is it important?
- How do you define a function in Dart?
- Which Dart function prints output to the screen?
- What is the
var
type? Arevar x = 5
andint x = 5
effectively the same? - What does the variable declaration
int? year
imply? - How do you call a constructor on a Dart class?
- What goes at the end of every Dart statement?
- When should you use
//
vs.///
for commenting? See also https://dart.dev/guides/language/effective-dart/documentation - What are the conventions for naming Dart classes, functions, and variables?
- can you have
var
type variables as class variables? - Are named parameters appearing between
{ }
always optional? - What does
@override
indicate about a method? - If A is a superclass of B, B is a subclass of A. Suppose A defines the method
foo()
. Can B callfoo()
?
Homework Assignment 1
You are now ready to make some inroads on your first homework assignment. Check Canvas for details.