mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-23 21:13:35 +00:00
Applied consistent code formatting across all packages using \$ melos format
└> dart format .
└> RUNNING (in 8 packages)
--------------------------------------------------------------------------------
benchmark_di:
Formatted 18 files (0 changed) in 0.30 seconds.
benchmark_di: SUCCESS
--------------------------------------------------------------------------------
cherrypick:
Formatted 24 files (0 changed) in 0.34 seconds.
cherrypick: SUCCESS
--------------------------------------------------------------------------------
cherrypick_annotations:
Formatted 11 files (0 changed) in 0.14 seconds.
cherrypick_annotations: SUCCESS
--------------------------------------------------------------------------------
cherrypick_flutter:
Formatted 3 files (0 changed) in 0.15 seconds.
cherrypick_flutter: SUCCESS
--------------------------------------------------------------------------------
cherrypick_generator:
Formatted 17 files (0 changed) in 0.27 seconds.
cherrypick_generator: SUCCESS
--------------------------------------------------------------------------------
client_app:
Formatted 4 files (0 changed) in 0.14 seconds.
client_app: SUCCESS
--------------------------------------------------------------------------------
postly:
Formatted lib/router/app_router.gr.dart
Formatted 23 files (1 changed) in 0.33 seconds.
postly: SUCCESS
--------------------------------------------------------------------------------
talker_cherrypick_logger:
Formatted 4 files (0 changed) in 0.18 seconds.
talker_cherrypick_logger: SUCCESS
--------------------------------------------------------------------------------
$ melos format
└> dart format .
└> SUCCESS. No functional or logic changes included.
123 lines
3.0 KiB
Dart
123 lines
3.0 KiB
Dart
import 'dart:async';
|
|
import 'package:cherrypick/cherrypick.dart';
|
|
|
|
class AppModule extends Module {
|
|
@override
|
|
void builder(Scope currentScope) {
|
|
bind<ApiClient>().withName("apiClientMock").toInstance(ApiClientMock());
|
|
bind<ApiClient>().withName("apiClientImpl").toInstance(ApiClientImpl());
|
|
}
|
|
}
|
|
|
|
class FeatureModule extends Module {
|
|
final bool isMock;
|
|
|
|
FeatureModule({required this.isMock});
|
|
|
|
@override
|
|
void builder(Scope currentScope) {
|
|
// Using toProvideAsync for async initialization
|
|
bind<DataRepository>()
|
|
.withName("networkRepo")
|
|
.toProvideWithParams((params) async {
|
|
print('REPO PARAMS: $params');
|
|
|
|
final client = await Future.delayed(
|
|
Duration(milliseconds: 1000),
|
|
() => currentScope.resolve<ApiClient>(
|
|
named: isMock ? "apiClientMock" : "apiClientImpl",
|
|
),
|
|
);
|
|
|
|
return NetworkDataRepository(client);
|
|
}).singleton();
|
|
|
|
// Asynchronous initialization of DataBloc
|
|
bind<DataBloc>().toProvide(
|
|
() async {
|
|
final repo = await currentScope.resolveAsync<DataRepository>(
|
|
named: "networkRepo",
|
|
params: 'Some params',
|
|
);
|
|
return DataBloc(repo);
|
|
},
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<void> main() async {
|
|
try {
|
|
final scope = CherryPick.openRootScope().installModules([AppModule()]);
|
|
|
|
final subScope = scope
|
|
.openSubScope("featureScope")
|
|
.installModules([FeatureModule(isMock: true)]);
|
|
|
|
// Asynchronous instance resolution
|
|
final dataBloc = await subScope.resolveAsync<DataBloc>();
|
|
dataBloc.data.listen(
|
|
(d) => print('Received data: $d'),
|
|
onError: (e) => print('Error: $e'),
|
|
onDone: () => print('DONE'),
|
|
);
|
|
|
|
await dataBloc.fetchData();
|
|
} catch (e) {
|
|
print('Error resolving dependency: $e');
|
|
}
|
|
}
|
|
|
|
class DataBloc {
|
|
final DataRepository _dataRepository;
|
|
|
|
final StreamController<String> _dataController = StreamController.broadcast();
|
|
Stream<String> get data => _dataController.stream;
|
|
|
|
DataBloc(this._dataRepository);
|
|
|
|
Future<void> fetchData() async {
|
|
try {
|
|
_dataController.sink.add(await _dataRepository.getData());
|
|
} catch (e) {
|
|
_dataController.sink.addError(e);
|
|
}
|
|
}
|
|
|
|
void dispose() {
|
|
_dataController.close();
|
|
}
|
|
}
|
|
|
|
abstract class DataRepository {
|
|
Future<String> getData();
|
|
}
|
|
|
|
class NetworkDataRepository implements DataRepository {
|
|
final ApiClient _apiClient;
|
|
final _token = 'token';
|
|
|
|
NetworkDataRepository(this._apiClient);
|
|
|
|
@override
|
|
Future<String> getData() async => await _apiClient.sendRequest(
|
|
url: 'www.google.com', token: _token, requestBody: {'type': 'data'});
|
|
}
|
|
|
|
abstract class ApiClient {
|
|
Future sendRequest({String url, String token, Map requestBody});
|
|
}
|
|
|
|
class ApiClientMock implements ApiClient {
|
|
@override
|
|
Future sendRequest({String? url, String? token, Map? requestBody}) async {
|
|
return 'Local Data';
|
|
}
|
|
}
|
|
|
|
class ApiClientImpl implements ApiClient {
|
|
@override
|
|
Future sendRequest({String? url, String? token, Map? requestBody}) async {
|
|
return 'Network data';
|
|
}
|
|
}
|