At its heart, Dart is a conservative programming language. It was not designed to champion bold new ideas, but rather to create a predictable and stable programming environment. The language was created at Google in 2011, with the goal of unseating JavaScript as the language of the web.
JavaScript is a very flexible language, but its lack of a type system and misleadingly simple grammar can make projects very difficult to manage as they grow. Dart aimed to fix this by finding a halfway point between the dynamic nature of JavaScript and the class-based designs of Java and other object-oriented languages. The language uses a syntax that will be immediately familiar to any developer who already knows a C-style language.
This chapter also assumes that Dart is not your first programming language. Consequently, we will be skipping the parts of the Dart language where the syntax is the same as any other C-style language. You will not find anything in this chapter about loops, if statements, and switch statements; they aren't any different here from how they are treated in other languages you already know. Instead, we will focus on the aspects of the Dart language that make it unique.
In this chapter, we will cover the following recipes, all of which will function as a primer on Dart:
- Declaring variables – var versus final versus const
- Strings and string interpolation
- How to write functions
- How to use functions as variables with closures
- Creating classes and using the class constructor shorthand
- Defining abstract classes
- Implementing generics
- How to group and manipulate data with collections
- Writing less code with higher-order functions
- Using the cascade operator to implement the builder pattern
- Understanding Dart Null Safety