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

Question: Don’t give up until the last moment. Regardless of whether the ending is successful or not, as long as you work hard and try your best, you will have a clear conscience.


This article describes the basic use knowledge points and the contrast between the const keyword and the final keyword in Dart

Insert picture description here

** You may need
CSDN NetEase Cloud Classroom Tutorial
Nuggets EDU Academy Tutorial
Know almost Flutter series articles
Headline sync Baidu Sync

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


1 const and final usage scenarios

1.1 final

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

Application scenario one is shown in the following figure. When final is used in a class, you must initialize and assign a value
Insert picture description here
when declaring a variable. When final is used in StatefulWidget, it is used to modify variables: in
Insert picture description here
this case, use final modified variables to see It seems that there is no initial assignment, but it does not violate the understanding of the assignment of final modified variables at runtime, because only when the defined StatefulWidget is initialized and used, the final modified variables will be assigned, 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 contents of modified variables are also called constants.

The core idea is: Assignment at runtime, can only be assigned once


1.2 Usage scenarios of const

const can be used to modify variables and constant constructors. The modified variables can only be assigned once.

Variables that can be modified by const can only be assigned once. Variables modified by const will be immutable constants throughout the life cycle of the application, and will only be created once in memory. Each subsequent call will be reused. The object created for the first time.

///全局常量 声明
const String name = "张三";

class TestPage2 {
    
    

  ///类常量 规范要求必须使用 static 修饰
  static const String name = "张三";

  void test(){
    
    
    ///方法块常量
    const  String name = "张三";
  }

}

const can be used to modify the constant constructor, often used to define enumeration types, as shown in the following code listing 1-2-1

/// 代码清单 1-2-1 
///订单类型 
class OrderStatus {
    
    

  static const OrderStatus notDeliver = const OrderStatus("待发货",1);
  static const OrderStatus hasBeenShipped = const OrderStatus("已发货",2);
  static const OrderStatus haveTheGoods = const OrderStatus("已收货",2);
  
  const OrderStatus(this.statusName,this.statusNumber);

  final String statusName;
  final int statusNumber;
}

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

///  代码清单 1-2-2
class TestClass5{
    
    

  ///判断订单类型
  void test(OrderStatus orderStatus){
    
    

    switch (orderStatus) {
    
    
      case OrderStatus.notDeliver:
        break;
      case OrderStatus.hasBeenShipped:
        break;
      case OrderStatus.haveTheGoods:
        break;
      default:
        break;
    }
  }
}

2 The difference between final and const

2.1 Final and const modified variables have different value timings

The so-called value timing is different, it means that the const modified variable is the value determined at compile time, and the final modified variable is determined at runtime.

The const modified variable has a certain value at compile time and before the program runs.

The value of a constant modified with const must be a result that can be calculated at compile time. The value that needs to be obtained at runtime cannot be modified, as shown in the following figure:
Insert picture description here

2.2 Different application areas

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

2.3 Create different objects for the same content

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:

  ///  代码清单 2-3-1
  ///单元测试
  test(" 测试1 ", () {
    
    
    
    const List<int> list1 = [1, 2, 3];
    const List<int> list2 = [1, 2, 3];

    // 输出结果为true 比较的是指针指向
    print(list1 == list2);

    // 输出结果为true 比较指针指向同时也比较内容
    print(identical(list1, list2));

  });

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

  ///  代码清单 2-3-2
  ///单元测试
  test(" 测试2 ", () {
    
    
    final List<int> list1 = [1, 2, 3];
    final List<int>  list2 = [1, 2, 3];

    // 输出结果为false 比较的是指针指向
    print(list1 == list2);

    // 输出结果为false 比较指针指向同时也比较内容
    print(identical(list1, list2));


  });

Public account my big front-end career

Guess you like

Origin blog.csdn.net/zl18603543572/article/details/108328115