Module design
- Dart course
- Intermediate
- Lesson 13/116
- 6 min
Design the skeleton of the helper library class.
Project: Helper library
In this 5-lesson project you'll build a reusable helper library. Each lesson adds one piece, and by the final lesson you'll have a working module.
You'll use what you learned in earlier groups: classes, constructors and final fields. Throughout the project you'll design a Helper class: a structure that bundles string, number and date helper methods in one package. Each lesson adds one method, and in the final lesson we call them all together.
Helper class skeleton
We'll start the project with a class named Helper. We'll add a final field to its body: version. This shows the library version.
Example: class Helper { final String version; Helper() : version = '1.0'; }. Since version is final, it is initialized via the initializer list. The Helper() constructor takes no parameters; the version is always '1.0'. An instance is created with var h = Helper(); and the version is read via h.version.
Practice Define the Helper class
Write the first piece of this project. Define a class named Helper. Add a final String version; field in the body. Write the Helper() : version = '1.0'; constructor. In main, create an instance with var h = Helper(); and print the version with print(h.version).
Starter code
// Define the Helper class
// Add a final version field and a constructor
void main() {
// Create a Helper instance and print h.version
}Sample solution
class Helper {
final String version;
Helper() : version = '1.0';
}
void main() {
var h = Helper();
print(h.version);
}Expected output
1.0
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Why bundle helpers in a class?
- It bundles related methods in one unit, easing reuse Correct answer
- It slows code down but improves readability
- It is only used for file operations
- It makes variable definitions unnecessary
Explanation: A class groups related methods in one unit, so you can reuse the code in other projects.
2 Complete the class declaration
class ____ {
final String version;
Helper() : version = '1.0';
}
class Helper {
final String version;
Helper() : version = '1.0';
}
Explanation: Class names use PascalCase; Helper starts with an uppercase letter.
3 Match the class parts
| Term | Meaning |
|---|---|
Helper |
Class name |
version |
Final field |
h.version |
Access via instance |