It's a simple interface to manage all instances that needs to been disposed.
import 'dart:async';
import 'package:disposer/disposer.dart';
class DataSource with Disposable {
final streamController = StreamController.broadcast();
@override
void dispose() {
streamController.close();
}
}
import 'dart:async';
import 'package:disposer/disposer.dart';
void main() {
final LocalDataSource localDataSource = LocalDataSource();
final RemoteDataSource remoteDataSource = RemoteDataSource();
final DataRepository dataRepository = DataRepository(
localDataSource: localDataSource,
remoteDataSource: remoteDataSource,
);
final DataUsecase dataUsecase = DataUsecase(repository: dataRepository);
dataUsecase.dispose();
// Output:
/*
[log] LocalDataSource Disposed
[log] RemoteDataSource Disposed
[log] DataRepository Disposed
[log] DataUsecase Disposed
*/
}
class LocalDataSource with Disposable {
final streamController = StreamController.broadcast();
@override
void dispose() {
streamController.close();
super.dispose();
}
}
class RemoteDataSource with Disposable {
final streamController = StreamController.broadcast();
@override
void dispose() {
streamController.close();
super.dispose();
}
}
class DataRepository with Disposable {
DataRepository({
required this.localDataSource,
required this.remoteDataSource,
});
@override
List<Disposable> get disposables => [
localDataSource,
remoteDataSource,
];
final LocalDataSource localDataSource;
final RemoteDataSource remoteDataSource;
}
class DataUsecase with Disposable {
DataUsecase({
required this.repository,
});
@override
List<Disposable> get disposables => [
repository,
];
final DataRepository repository;
}
Activate the global pub package. With this approach, you can use features generator globally
Activate:
dart pub global activate disposer
Usage:
disposer --name NewFeature --output lib/src/features
Add disposer like dependency to your project
dependencies:
disposer: ^0.0.7
Usage:
flutter pub run disposer --name NewFeature --output lib/src/features
Feature naming convention:
-n example
--name example
-n Example
--name Example
-n ExampleFeature
--name ExampleFeature
Output parameter is optional. Default path is: lib/features
.
Example:
-o lib/src/features
--output lib/src/features