What is Dart?
Hi, in this article I want to give you a brief introduction to the Dart programming language. From reading this article, you'll get the general meaning of the Dart and where it is used.
Let's jump into the story behind the Dart. Dart is designed by Lars Bak and Kasper Lund and developed by Google. As may you've noticed or not, Dart has a logo with a pretty unique design. If we take a closer look, we can see that it represents the end of the dart arrow.
"A player can throw this dart arrow to a dartboard during a dart game". Imagine the Dartboard, and let's say it's the entire ecosystem of Dart-L, the Dart language is the arrow that the developer holds.
From this story, we can extract some meaning and decide that Dart language should represent the following standards: precision — be optimized as possible; speed — be minimalist and fast to run; tough — be scalable, maintainable and readable; modifiable — benefit of hot reload; use a popular framework — the foundation of Flutter (we'll learn Flutter at the next upcoming chapters).
What is Dart programming language used for?
Dart is a client-optimized language for developing fast apps on any platform. Its goal is to offer the most productive programming language for multi-platform development, paired with a flexible execution runtime platform for app frameworks. Dart is an object-oriented programming language and is widely used in Flutter - a UI toolkit from Google for building UI and UX interactions and app components on Android/IOS, Desktop and Web.
The Dart language is type-safe. This means, that the only operations that can be performed on data in Dart are those allowed by the type of data.
void main() {
String b = "Hello World!";
b = 5;
print(b);
}
We'll get the following error A value of type 'int' can't be assigned to a variable of type 'String'.
The Dart type system
It uses a combination of static type checking and runtime checks to ensure that a variable’s value always matches the variable’s static type. Sometimes, this is referred to as sound typing. Although types are mandatory, type annotations are optional because of type inference.
Runtime checks occur only at the execution of the code and go always after the static type checks.
Type inference
The analyzer can infer types for fields, methods, local variables, and most generic type arguments. When the analyzer doesn't have enough information to infer a specific type, it uses the dynamic
type.
void main() {
dynamic a = 7; //int
print(a.runtimeType);
a = 'test'; //String
print(a.runtimeType);
a = 7.1; //double
print(a.runtimeType);
}
Using dynamic
type can be useful during experimentation or for code that needs to be especially dynamic. The dynamic
type itself is static but can contain any type at runtime.
As you've noticed we can declare a variable without directly specifying its type. We can use the var
keyword Dart to assign the type to a value. Unlike in dynamic
, the variable message
can't contain the value of any type except String as we assigned this type to it.
void main() {
var message = "Welcome to FlutterDev's blog!";
print(message.runtimeType); //String
}
Dart language comes with the sound null safety. In Dart 3, you always get null safety. With sound null safety, variables are 'non-nullable' by default. You can assign values to the variable of declared types and never assign null.
Although, you can specify a type of variable to be null (e.g. int?
), and only then the variable can be either null or not null.
void main() {
int? pair;
print(pair.runtimeType); //Null
}
This is all for today! Hope you discovered and learned something for yourself!
In the next article, we'll talk about the existing data types, variables and compilers in Dart.