Encapsulates related code into a self-contained unit, exposing only a public API while hiding internals.
Dart has this built-in — every .dart file is a module, and _ prefix makes members private.
// auth_module.dart
// private — hidden from importers
String _secretKey = 'abc123';
String _hashPassword(String password) => 'hashed_$password';
// public API — visible to importers
class AuthModule {
static bool login(String user, String password) {
final hashed = _hashPassword(password);
print('Logging in $user with key $_secretKey');
return hashed.isNotEmpty;
}
static void logout() => print('Logged out');
}
// main.dart
import 'auth_module.dart';
void main() {
AuthModule.login('alice', 'pass123'); // works
AuthModule.logout(); // works
// _secretKey; — compile error, private
// _hashPassword(); — compile error, private
}