How to initialize variables in constructor body in Dart

⋅ 3 min read ⋅ Flutter Dart Constructor

Table of Contents

How to initialize variables in constructor body in Dart

In Dart, we must initialize all instance variables before a constructor body is executed.

This is the main difference and the cause of confusion between Dart and other programming languages.

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

Non-nullable instance field '...' must be initialized

If you try to initialize instance variables in a constructor body, you will find out that it is not as easy as you normally do in other programming languages.

Here is an example where I try to initialize all instance variables in a constructor body.

class DoublePoint {
double x;
double y;

DoublePoint(
double x,
double y,
) {
this.x = x * 2;
this.y = y * 2;
}
}

You will get the following error.

Non-nullable instance field 'x' must be initialized.
Try adding an initializer expression, or add a field initializer in this constructor, or mark it 'late'

Luckily, the error also tells us three ways to solve this.

  1. Adding an initializer expression.
  2. Add a field initializer in this constructor.
  3. Mark it late.

Let's see what we have to do for each solution.

Try adding an initializer expression

We can silence the error by initialize all instance variables when we declare them.

This solution is straightforward, since Dart requried all fields to be initialized before entering a constructor body, we do it at the declaration time.

class DoublePoint {
double x = 0;
double y = 0;

DoublePoint(
double x,
double y,
) {
this.x = x * 2;
this.y = y * 2;
}
}

Add a field initializer in this constructor

I think this is the solution that most people who aren't familiar with Dart are looking for.

Dart has a unique syntax to initialize each field in a constructor.

You initialize the field in the Initializer list, which is placed before a constructor body.

It has the following syntax:

  1. You put a colon (:) before a constructor body.
  2. You assign each instance variable after the colon.
  3. Separate each expression with a comma (,).
ClassName(param1, params2, ...) 
: field1 = ..., field2 = ...., ....
{
// Constructor body
}

Here is the same example with each field initialized in the initializer list.

What you need to do is basically move field initializers out of the constructor body to the special place before the body.

class DoublePoint {
double x;
double y;

DoublePoint(
double x,
double y,
) : x = x * 2,
y = y * 2 {
print('this.x: ${this.x}');
print('x: $x');
print('this.y: ${this.y}');
print('y: $y');
}
}

DoublePoint(1, 2)
// this.x: 2.0
// x: 1.0
// this.y: 4.0
// y: 2.0

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

Mark it late

Adding the late modifier on a variable tells a compiler that we will make sure it is initialized before being used.

We add the late keyword on all instance variables so we can initialize them lazily in the constructor body.

class DoublePoint {
late double x;
late double y;

DoublePoint (
double x,
double y,
) {
this.x = x * 2;
this.y = y * 2;
}
}

The last solution is quite dangerous, in my opinion. Adding the late is kind of skipping the Dart requirement and promising we will fulfill it later. Failing to fulfill that will cause a runtime error.


Read more article about Flutter, Dart, Constructor, or see all available topic

Enjoy the read?

If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Become a patron Buy me a coffee Tweet Share
Previous
How to add a TextField to Alert in SwiftUI

iOS 16 add the ability to integrate a text field in an alert.

Next
SwiftUI Grid

iOS 16 add a new Grid view to SwiftUI. A Grid view arranges child views in rows and columns. This table-like structure makes a layout that is hard to do in vertical and horizontal stacks become easier.

← Home