Become a member
to unlock all features

    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.


    Learn more at https://dartlang.org



    Become a Member to view code

    You must be a Member to view code


    Become a Member
    and unlock code for this lesson


    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.