Sub topic 3
Privacy with a leading underscore
Assume you know Dart variables, methods, classes, and null safety. This chapter explains how Dart hides implementation details across library boundaries.
What a leading underscore means
In Dart, an identifier that starts with _ is private to its library. A library is usually one Dart file. Files joined with part and part of also belong to the same library.
Privacy can apply to:
- Top-level variables and functions.
- Class fields.
- Class methods.
- Getters, setters, and constructors.
A private member can still be used by every class and function in the same library. Code in another library cannot access it directly.
class Wallet {
final String owner;
double _balance;
Wallet(this.owner, double startingBalance)
: _balance = startingBalance;
double get balance => _balance;
void deposit(double amount) {
if (amount <= 0) {
throw ArgumentError('Amount must be positive');
}
_balance += amount;
}
bool withdraw(double amount) {
if (amount <= 0 || amount > _balance) {
return false;
}
_balance -= amount;
return true;
}
}
void main() {
final wallet = Wallet('Mina', 50);
wallet.deposit(25);
wallet.withdraw(10);
print('${wallet.owner} has ${wallet.balance}');
}The _balance field cannot be accessed directly from another library. The public balance getter and the deposit and withdraw methods provide controlled access.
Privacy is library-level
Dart does not use the leading underscore to create a class-only private member. Other classes in the same library can access that member.
For example, a helper function in the same file could read wallet._balance. This can be useful when several classes form one implementation, but it also means that privacy is broader than in languages with private fields.
A leading underscore also does not automatically protect data from changes. A private field can be mutable, as _balance is in the example. Use final when a value must not be reassigned after construction.
| Declaration | Accessible from the same library | Accessible from another library |
|---|---|---|
| `String name` | Yes | Yes |
| `String _token` | Yes | No |
| `final String id` | Yes | Yes, but not reassignable |
| `final String _id` | Yes | No |
Choosing a public boundary
Keep internal state private when callers should not change it freely. Expose operations that enforce rules instead of exposing a mutable field.
A public getter can reveal a value without allowing assignment. A private setter can allow controlled updates inside the library:
class Temperature {
double _celsius;
Temperature(this._celsius);
double get celsius => _celsius;
set celsius(double value) {
if (value < -273.15) {
throw ArgumentError('Temperature cannot be below absolute zero');
}
_celsius = value;
}
}The setter is public in this example. If it were named _celsius, outside code could read the value only through celsius, while the library could update the private field directly.
Exercise
Why might a class expose a withdraw method instead of a public _balance replacement that callers can change directly?
Show answer
The method can validate the amount and preserve the class's rules before changing its private state.