How does a const constructor differ from a standard constructor?

Asked

Viewed 165 times

1

In the documentation it says that the builder const Creates an object that will never change. All vars has to be final.

So it means that I will have the same result in creating the following classes?

class Produto {
  const Produto({this.nome});
  final String nome;
}

class Produto2 {
  Produto2({this.nome});
  final String nome;
}

How does the const constructor concept actually work? How can a const constructor be used advantageously over a normal constructor?

Based on the question: How does the const constructor Actually work?

2 answers

2


Reading the answers linked it seems that not even they understood very well for what it serves :) But the conclusion is that, roughly speaking, it functions as a named tuple. It serves for you to create a kind of literal of a type, so you can use the constructor as that literal where you create the object normally but this syntax allows the compiler to interpret the data as a literal and can analyze it better at compile time, or it understands how the object will behave at runtime already at compile time and can do some operations that depend on it or do optimizations.

It shows that the builder is not actually a builder as we know, it has no implementation and does nothing specific, not least because it probably does something standard, so the compiler already knows what to do, and to ensure that the literal keeps the object state all the time all fields of the object must be final, ie constant, and of course, all fields need to be initialized in one way or another.

They use the term canonize the object because if they have the same data they are considered the same object. Canonical objects known at compile-time can be used in places where it only accepts literals, such as enumerations, values of switch, annotations, in addition to already cited improve performance, including there a comparison be made directly without involving processing at runtime.

Internally it must treat the object as a single thing, just as an integer is a single thing and can be compared directly, it is a way of showing that the object is something static and simple, without special rules.

1

Without going into details and wild theories that few will read, you are directing the question to Dart, and I will direct it specifically to Flutter (which is Dart’s main use today).

When we access the SDK, we will witness several uses of constructors const among the Widgets, mainly the StatelessWidgets, in fact, if we create a StatelessWidget with attributes final and ask the IDE to create a constructor:

inserir a descrição da imagem aqui

It will automatically create a constructor const:

inserir a descrição da imagem aqui

And paraphrasing the dart documentation:

CONSIDER making your constructor const if the class Supports it.

If you have a class Where all the Fields are final, and the constructor does Nothing but initialize them, you can make that constructor const. That Lets users create instances of your class in Places Where constants are required-Inside other Larger constants, switch cases, default Parameter values, etc.

In addition to being a good practice, Flutter benefits greatly from this for its performance. That’s because a Statelesswidget built through a builder const can be reused in the Widgets tree, because this created object is canonicalized, which means that no matter how many times this constant value is called in the code, there will always be only one object in memory. For example this code where we create 2 List objects with const in Dart, see here in practice:

main() {
  var a = const [1,2,3];
  var b = const [1,2,3];
  print('A e B são os mesmos objetos? ${identical(a, b)}');
}

What in Flutter happens a lot in cases of addition of padding with the famous const EdgeInsets.all(8.0). Imagine adding the same padding value to various places in the Widgets tree.

Another issue is that since this object is immutable, its values are final and it was built with a builder const, when rebuilding the Widgets tree, these Widgets const that obviously have not been changed, do not need to be reconstructed, taking place an optimization in the use of setState().

  • Good answer, please repeat the answer to this question: https://answall.com/questions/403700/como-o-uso-de-constructors-cons-pode-best_a-performance-no-flutter @Julio-Henrique-Bitencourt

Browser other questions tagged

You are not signed in. Login or sign up in order to post.