Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Understand Variables and Constants in Dart

    Jermaine OppongJermaine Oppong

    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



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Instructor: 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.

    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.

    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.

    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.

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