Flutter study notes: usage of identical function and characteristics of constant constructor

identical function

usage:

bool identical(
Object? a,
Object? b
)

Function: Check whether two references point to the same object, simply to check whether two instances share a storage space

Example 1:

  var o1= new Object();
  var o2= new Object();
  
  print(identical(o1,o2)); //false
  print(identical(o1,o1));  //true

It can be seen that o1 and o2 do not share storage space

Example 2:

print(identical([2],[2]));//false
  
  var a=[2];
  var b=[2];
  
  print(identical(a,b));//false

Likewise, two lists of equivalent values ​​do not share storage space.

However, there is a special case const:

When the const keyword creates the same object in multiple places, only one object is kept in memory

Example 3:
const Object() means to instantiate a constant constructor

  var o1=const Object();
  var o2=const Object();
  print(identical(o1,o2)); //true
  print(identical(o1,o1));  //true


  print(identical(const[2],const[2]));//true
  
  const a=[2];
  const b=[2];
  print(identical(a,b));//true

Then we can see that the prerequisites for shared memory space are:

1. Constant
2. Equal value

The characteristics of the constant constructor

Here is the Container class we defined:

class Container{
    
    
  int width;
  int height;
  Container({
    
    required this.width,required this.height});
  //这是一个普通的构造函数
}


void main(){
    
    
  var c1=new Container(width: 100,height: 100);
  var c2=new Container(width: 100,height: 100);
  print(identical(c1,c2));//false
  
}

Here's the container class with a constant constructor:

class Container{
    
    
  final int width;
  final int height;
  const Container({
    
    required this.width,required this.height});
}


void main(){
    
    
  var c1=new Container(width: 100,height: 100);
  var c2=new Container(width: 100,height: 100);
  print(identical(c1,c2));//false
  
  
  var c3=const Container(width: 100,height: 100);
  var c4=const Container(width: 100,height: 100);
  print(identical(c3,c4));//true
  
}

So we can know:

1. The constant constructor needs to be modified with the const keyword

2. The const constructor must be used for classes whose member variables are all final

3. If the const modifier is not added when instantiating, even if the constant constructor is called, the instantiated object is not a constant instance

4. When instantiating the constant constructor, the object is created in multiple places. If the value passed in is the same, only one object will be retained

5. In Flutter, const decoration is not only to save memory overhead when building components. Flutter will not rebuild const components when it needs to rebuild components (const decorated components will not be built when rebuilding)

Using constant constructors in specific cases can greatly save memory space and effectively improve application performance

Guess you like

Origin blog.csdn.net/weixin_46136019/article/details/128982791