Builder with Dart

Flutter tips Published on

Dart makes working with objects simple, often letting you avoid extra code commonly used in other languages.

The "Useless" Approach

Sometimes, developers create extra classes or methods just to set properties on an object in a chained way. The image shows an example of a MyModelBuilder class with methods like setName, setTitle, and setCount that all return the builder itself, ending with a build method.

Why It's Useless in Dart

Dart has built-in features that make this kind of builder pattern unnecessary for simple object creation and configuration.

The Dart Way: Cascade Notation

What is Cascade Notation?

Dart introduces a special syntax called "cascade notation", represented by ... This notation is incredibly useful.

How to Use It

You can directly use the cascade notation to make a sequence of operations on the same object. Look at the example main function in the image. Instead of creating a MyModelBuilder, you can create the MyModel directly and then use ..name, ..title, and ..count to set its properties.

Cleaner Code

This approach is much cleaner because you don't need to write separate methods (like setName, setTitle, etc.) that just return this. You simply chain the operations using ...

Benefit

Dart's cascade notation lets you achieve the same goal as a traditional builder (configuring an object step-by-step) with less code and better readability for simple cases.

Save 3 months of work

Create your app using our 6 years of making Flutter apps and more than 50+ apps

kickstarter for flutter apps

Frequently Asked Questions

What is a builder pattern?

It's a design pattern used to construct complex objects step-by-step, where building methods often return the builder object itself to allow chaining.

What is Dart's cascade notation?

It's a syntax (`..`) that allows you to perform a sequence of operations (like method calls or property assignments) on the same object without repeating the object's name.

Why might a traditional builder be unnecessary in Dart?

Dart's cascade notation provides a cleaner, built-in way to configure objects by chaining operations, reducing the need for a separate builder class for simple cases.

How do you use cascade notation?

Start with the object, then use `..` followed by the method call or property assignment. You can chain multiple operations with `..`.

Read more
You may also be interested in
How to create a timer  blog card image
How to create a timer
Published on 2025-06-18T12:06:30.487Z
Fetching data using http client  blog card image
Fetching data using http client
Published on 2025-06-18T12:06:52.625Z
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved