Dart Const Constructor

Summary: in this tutorial, you’ll learn about the Dart const constructor and how to use it to make all objects of a class constant at compile time.

Introduction to the Dart const constructor

Immutability means being unable to be changed. If a value is immutable, that value cannot be changed.

To make a field of a class immutable, you can use either the const or final keyword.

If you use the const keyword, you need to initialize the value in the field declaration. Also, the value must be known at compile time.

However, if you use the final keyword, you can assign a constant value to the field once at runtime:

final type fieldName;Code language: Dart (dart)

In this syntax, you need to assign a value to the fieldName once in the constructor. Once you create an object, the fieldName is immutable.

The following example defines the Text class that has an immutable property:

class Text {
  final String content;
  Text({this.content = ''});
}

void main() {
  const text = Text(content: 'Hello');
  print(text.content);
}Code language: Dart (dart)

Output:

HelloCode language: Dart (dart)

How it works.

First, define the Text class and use the final keyword to mark the content property as immutable:

final String content;Code language: Dart (dart)

Second, initialize the content property in the constructor:

Text({this.content = ''});Code language: Dart (dart)

Note that if a property is final, you cannot assign a value to it in the constructor’s body. For example, the following will cause an error:

class Text {
  final String content;

  Text(String content) {
    this.content = content;
  }
}Code language: Dart (dart)

Third, create a new Text object and display the value of the content property:

var text = Text(content: 'Hello');
print(text.content);Code language: Dart (dart)

The content property of the text object is immutable. If you attempt to assign a new value to it, you’ll get an error:

// error
text.content = 'Bye';Code language: Dart (dart)

Error:

'content' can't be used as a setter because it's final.Code language: Dart (dart)

If all fields of a class are final, then the object of the class will never change. If so, you can use a constant constructor to ensure all instances of the class will be constants at compile time.

For example, the Text class has the content property as final, it is a good candidate for the constant constructor.

To make a constructor constant, you add a const keyword before the constructor name like this:

class Text {
  final String content;
  const Text({this.content = ''});
}Code language: Dart (dart)

And you can create a new object of the Text class as follows:

void main() {
  var text = Text(content: 'Hello');
  print(text.content);
}Code language: Dart (dart)

Put it all together.

class Text {
  final String content;
  const Text({this.content = ''});
}

void main() {
  var text = Text(content: 'Hello');
  print(text.content);
}Code language: Dart (dart)

When you use the constant constructor, Dart will optimize the memory so that if multiple objects have the same property values, Dart will create only a single instance. For example:

class Text {
  final String content;
  const Text({this.content = ''});
}

void main() {
  var text1 = const Text(content: 'Hello');
  var text2 = const Text(content: 'Hello');
  print(identical(text1, text2)); // true
}Code language: Dart (dart)

Output:

trueCode language: Dart (dart)

How it works.

First, create two Text objects with the same content property:

var text1 = const Text(content: 'Hello');
var text2 = const Text(content: 'Hello');Code language: Dart (dart)

Notice that the const keyword appears before the Text() constructor.

Second, use the identical() function to check if both objects are the same:

print(identical(text1, text2));Code language: Dart (dart)

The result is true because text1 and text2 have the value for the content property.

Summary

  • Use the final keyword to make a field of a class immutable.
  • Use a constant constructor to make all objects of a class constant at compile time.
Was this tutorial helpful ?