A detailed explanation of constants (const | final) in the Dart language ๐Ÿ’ฅ


 In the Dart language, a special keyword is used to define constants, which are values ​​that cannot be changed after they are initialized. Constants are useful for several reasons, including: 


 ▪️Performance improvement: Constants improve program performance by enabling the code compiler to be optimized to code more efficiently. 


 ▪️Make the code more readable: Constants help make the code clearer and more understandable, as they clearly indicate which values ​​do not change. 


 ▪️Error prevention: Constants help prevent errors that may arise from accidentally changing the value of a variable. 


 ๐Ÿ”ฐ Types of constants in Dart: 


 There are two main types of constants in Dart: 


 ๐Ÿ”น️const: used to define constants whose value is set at compile-time. 


 ๐Ÿ”ธ️ final: used to define constants whose value can be set at runtime or at compile-time. 


 ๐Ÿ’  Rules for using constants: 


 • Constants must be assigned a value when defined. 


 • The value of a constant cannot be changed after it has been allocated. 


 • Constants can be used anywhere variables can be used. 


 • Constants are fixed values ​​at the same time, which means that their value does not change even between different runs of the program. 


 ✅ How to define constants: 


 1️⃣ Use of the constant const: 


 The const keyword is used to define constants whose value is set at runtime. 


 ๐Ÿ”ป Advantages of using const: 


 • Improved performance: Constants declared using const improve program performance more than constants declared using final. 


 • Value inference capability: The compiler can infer the value of a constant defined using const at runtime, allowing it to further optimize the code. 


 • Prevent reset: The value of a constant defined using const cannot be reset. 


 ❓When do we use const? 


 • For previously known values: such as the value of the mathematical constant “pi”. 


 • For values ​​that do not change while the program is running: such as a file path or server name. 


 • To create constant objects: using a constant constructor. 


 ๐Ÿ’  Examples of using const: 


 const double pi = 3.14159; // Definition of a mathematical constant

 const String message = 'Hello, world!'; // Define a text constant

 const List<int> numbers = [1, 2, 3]; // Constant definition of a list 


 class Person {

    final String name; // Define a constant within a class 


    const Person(this.name);

 } 


 ๐Ÿ”น️Important notes: 


 • Const cannot be used to define dynamic constants. 


 • Const cannot be used to define constants that depend on the values ​​of other variables. 


 2️⃣ Use of the constant final: 


 The final keyword is used to define constants whose value can be set at program runtime or at code runtime. 


 ๐Ÿ”ป Features of using final: 


 • Prevent reset: The value of a constant defined with final cannot be reset after it is set for the first time. 


 • Can be used with different data types: final can be used with any data type. 


 ❓ When do we use final?


User or temporary file path. 


 • To prevent the value of a variable from being accidentally changed: 


 ๐Ÿ’  Examples of use of final: 


 final String name = 'Pro3Dev'; // Define a text constant

 final int age = 30; // Definition of a numerical constant 


 void main() {

    final message = 'Welcome to Dart!';

    print(message);

 } 


 ๐Ÿ”ธ️Important notes: 


 • final can be used to define constants of any data type, including dynamic. 


 • final can be used to define constants that depend on the values ​​of other variables. 


 ๐Ÿงฎ Various examples of using constants: 


 ☑ Definition of mathematical constants:

 const double pi = 3.14159;

 const double e = 2.71828; 


 ☑ Definition of file paths: 


 const String appDataPath = '/data/app/my_app/data/';

 const String assetsPath = 'assets/'; 


 ☑ Definition of color codes: 


 const Color primaryColor = Colors.blue;

 const Color accentColor = Colors.amber; 


 ๐Ÿ”น️Important notes: 


 • Constants cannot be used in assignment expressions. 


 • Constants cannot be used as parameters for construction functions. 


 • Constants cannot be used as a data type. 


 ๐Ÿ†š️ The difference between const and final: 


 ๐Ÿ”น️const: used to define constants whose value is assigned at compile time. 


 ๐Ÿ”ธ️ final: used to define variables whose value is assigned at runtime, but whose value cannot be changed afterwards. 


 ๐Ÿ”ป When do we use const and when do we use final? 


 • Use const: 


 • If the value of the constant is known before running the program. 


 • If you want to improve the performance of the program. 


 • Use final: 


 • If the value of the variable is unknown before running the program. 


 • If you want to prevent the value of a variable from changing after it has been assigned. 


 ♻️ General conclusion of the constants: 


 Constants are a powerful feature in the Dart language that helps improve program performance and make it more clear and readable. By understanding how to use constants correctly, you can write more efficient and secure code. 


 ๐Ÿงก I wish you all the best in your journey in the field of programming and development!

Comments

Popular Posts