Understand Variables and Constants in Dart

Jermaine Oppong
InstructorJermaine Oppong

Share this video with your friends

Send Tweet
Published 3 years ago
Updated a month ago

In this lesson, we will look at how to create variables and constants. These are containers that store data for later reference and/or retrieval.

Dart is an object-oriented programming language by Google, which aims to help the developer build modern web applications. It covers client, server and now mobile with Flutter. It comes with a range of tools including a virtual machine, core libraries, and package management repository, lending enough ammunition to start your next project.

Learn more at https://dartlang.org

Instructor: [00:00] Here is an example of a variable. Dart is able to infer the type of value assigned to our variable, which in this case is an integer, although we could be explicit by declaring the type.

[00:16] We can also set constant values, which essentially are values that cannot be changed once they are set. This comes in two forms. There's the final keyword. If we attempt to reset this to a different value, we will get an error.

[00:36] The second form of declaring constant values is using the const keyword. The difference in practice between const and final is you use final when certain instance variables belong into classes.

[00:49] Also, const is treated as a compile term constant, which means that any value we assign will be calculated during code compilation. Const variables must therefore be initialized with a valid const value, or an exception is thrown.

[01:10] Some of the valid const values are number, string, Boolean, array, map, symbol, and any constructors marked as const.