The difference between the Dart core language basic const keyword and the final keyword

Inscription: Don't give up until the last moment. No matter whether the ending is successful or not, as long as you have worked hard and tried your best, you will have a clear conscience.


This article describes the basic knowledge points and comparisons of the const keyword and final keyword in Dart

insert image description here

This article was first published on the WeChat public account (biglead), my big front-end career, and published in various technical forums simultaneously.


1 const and final usage scenarios

1.1 final

Final is used to modify variables that can only be assigned a value once, and is assigned at runtime. The so-called runtime is that when the program executes this piece of code, the final modified variable will be assigned a value.

Application scenario 1 is shown in the figure below. When final is used in a class, when declaring a variable, you must initialize and assign
insert image description here
it. When final is used in StatefulWidget, it is used to modify variables: in
insert image description here
this case, use final modified variables to see It seems that there is no initialization assignment, but it does not violate the understanding of final modified variables at runtime, because only when the defined StatefulWidget is initialized and used, the final modified variable will be assigned, and of course it is also assigned once.

So the conclusion is: final can be used to modify variables. Modified variables are assigned at runtime and can only be assigned once, so the content of modified variables is also called constants.

The core idea is: run-time assignment, can only be assigned once


1.2 Usage scenarios of const

const can be used to modify variables, modify constant constructors, and modified variables can only be assigned a value once.

Variables that can be modified by const can only be assigned once. Variables modified by const will be immutable constants in the entire life cycle of the compiler and the application, and will only be created once in memory, and will be reused for each subsequent call. Object created for the first time.

///Global constant declaration 
const String name =  "Zhang San" ;

class TestPage2 {

  ///The class constant specification requires that you use static to modify 
  static  const String name =  "Zhang San" ;

  void  test ( ) { 
    ///Method block constant 
    const   String name =  "Zhang San" ; 
  }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

const can be used to decorate constant constructors and is often used to define enumeration types, as shown in the following code listing 1-2-1

/// Code Listing 1-2-1 
/// Order Type 
class  OrderStatus  {

  static  const OrderStatus notDeliver =  const  OrderStatus ( "To Ship" , 1 ) ; 
  static  const OrderStatus hasBeenShipped =  const  OrderStatus ( "Shipped" , 2 ) ; 
  static  const OrderStatus haveTheGoods =  const  OrderStatus ( "Received" , 2 ) ;
  
  const OrderStatus(this.statusName,this.statusNumber);

  final String statusName;
  final int statusNumber;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

Use the following code as shown in Listing 1-2-2

/// Code Listing 1-2-2 
class  TestClass5 {

  ///Determine the order type 
  void  test ( OrderStatus orderStatus ) {

    switch (orderStatus) {
      case OrderStatus.notDeliver:
        break;
      case OrderStatus.hasBeenShipped:
        break;
      case OrderStatus.haveTheGoods:
        break;
      default:
        break;
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

2 The difference between final and const

2.1 The timing of final and const-modified variables is different

The so-called value timing is different, which means that the const-modified variable has its value determined at compile time, while the final-modified variable is determined at runtime.

A variable modified by const has a definite value at compile time, before the program runs.

The value of a constant decorated with const must be computable at compile time. It is not modifiable for the value that needs to be obtained at runtime, as shown in the following figure:
insert image description here

2.2 Different application areas

final can only be used to modify variables, the
const keyword can be used to modify variables and constant constructors

2.3 The same content object is created differently

The following code listing 2-3-1, although two objects list1 and list2 are created, but the content is the same and both are const objects, so they point to the same object:

  /// Code Listing 2-3-1 
  ///Unit test 
  test ( " test 1 " ,  ( )  {
    
    const List<int> list1 = [1, 2, 3];
    const List<int> list2 = [1, 2, 3];

    // The output result is true, the comparison is the pointer points to 
    print ( list1 == list2 ) ;

    // output result is true compare pointer to and also compare content 
    print ( identical ( list1 , list2 ) ) ;

  } ) ;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

The following code listing 2-3-2, the final declared variable, even if the content is the same, is to recreate a new object:

  /// Code listing 2-3-2 
  ///Unit test 
  test ( " test 2 " ,  ( )  { 
    final List < int > list1 =  [ 1 ,  2 ,  3 ] ; 
    final List < int >   list2 =  [ 1 ,  2 ,  3 ] ;

    // The output result is false compared to the pointer to 
    print ( list1 == list2 ) ;

    // output result is false compare pointer to and also compare content 
    print ( identical ( list1 , list2 ) ) ;


  } ) ;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

Public account my big front-end career

Tags: The difference between the Dart core language basic const keyword and the final keyword

A full set of tutorials for Flutter project development Dart const Flutter const

Related: The difference between the Dart core language basic const keyword and the final keyword