Before we dive into the code, let's diverge briefly to the builder pattern. Builders are a type of class whose only purpose is to build other classes. They are often used for complex objects with many properties. It can get to a point where standard constructors become impractical and unwieldy because they are too large. This is the problem the builder pattern solves. It is a special kind of class whose only job is to configure and create other classes.
This is how we would accomplish the builder pattern without the cascade operator:
class UrlBuilder {
String _scheme;
String _host;
String _path;
UrlBuilder setScheme(String value) {
_scheme = value;
return this;
}
UrlBuilder setHost(String value) {
_host = value;
return this;
}
UrlBuilder setPath(String value) {
_path = value;
return this;
}
String build() {
assert(_scheme != null);
assert(_host != null);
assert(_path != null);
return '$_scheme://$_host/$_path';
}
}
void main() {
final url = UrlBuilder()
.setScheme('https')
.setHost('dart.dev')
.setPath('/guides/language/language-tour#cascade-notation-')
.build();
print(url);
}
This is very verbose. Dart can implement this pattern without any setup.
Create a new file in your project or type the code of this recipe in Dartpad.