Dart Overview

Learn the basics of Dart programming language.

In this lesson, we’ll learn a few basics about Dart, a programming language designed to develop fast apps on any platform. It’s a client-side optimized language, unlike Java or JavaScript which are optimized for the server side. Dart also forms the foundation of Flutter. It provides the language and runtimes that power Flutter apps.

Those familiar with the Kotlin world will probably find it easy to understand Dart. Or, in general, those familiar with object-oriented languages probably won’t have any problem catching up with Dart.

Dart is a strongly typed language, which means most of the expressions (like variable assignment, function return types, etc.) have enforced strict data-type checking at the time of compilation. This implies that errors and exceptions are more likely to happen during compilation.

Variables

var count = 25;

If we use the var keyword instead of the actual data type, Dart will use its type inferencing feature to deduce the type. Here, it will deduce the type as int. Similarly, we can also declare the variable as follows:

int count = 25;

Since Dart has a type inference feature, it is advised that we don’t use the explicit data type and use the var keyword. But if we are not sure about the initialization at the time of declaring the variable, then we have to give its type.

int count;

When we use var or an actual data type like int here, then we are allowed to reassign the variable. But if we are sure that our variable won’t get reassigned once initialized, then, as a good practice, use the final keyword at the time of initialization.

final count = 25;

Functions

Functions in Dart are similar to functions in Java. The only difference is that the access specifier must be specified. To declare a private method, we have to start the function name with an underscore (_). Otherwise, it will be treated as a public function. In the following code snippet, we demonstrate how the main.dart file is not able to access the _privateGreet function.

Try running the demo as it is and then after uncommenting line 9 and line 10 to see and evaluate the difference.

Press + to interact
main.dart
another_class.dart
class AnotherClass {
String publicGreet() {
return "I am greeting you publicly.";
}
String _privateGreet() {
return "I am greeting you privately.";
}
}

This code defines a class called AnotherClass with two methods:

  • publicGreet: This is a public method, which means it can be accessed from outside the class.
  • _privateGreet: This is a private method, which means it can only be accessed within the class or by other classes in the same library.

The main function creates an instance of AnotherClass and calls the publicGreet method on it. It then prints the result to the console. The _privateGreet method is not called.

In object-oriented programming, it’s generally considered a best practice to make methods private if they aren’t intended to be used by external code. This helps to encapsulate the internal implementation details of a class and prevent external code from relying on them. This can make it easier to change the internal implementation without breaking external code.

The difference is pretty clear, right? As expected, private fields are not accessible from a different class.

final vs. const

The final keyword

We saw the final keyword when we were learning to declare variables. The final keyword is used to initialize variables that we want to be immutable, i.e., their value cannot be changed later. So final modifies variables.

final z = 3;
final x = DateTime.now();

The const keyword

The const keyword modifies values. This means whenever we have an object, variable, widget, etc., we know that its underlying value or state won’t change.

To have a better understanding of const is to think in a way that const object values can be calculated at compile time. For example, 1+2 will always be equal to 3, and this fact cannot be changed. But on the other hand, the DateTime.now() function will return a different DateTime on every single function call, even if there is a difference of a nanosecond from the previous DateTime.now() function call, and hence, it is dependent on runtime.

const text = Text('Hello, World!');

Note: A final list data type can still be immutable. We can add or delete elements from it, but the const list data type can’t be changed. All the members have to be fixed.