mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-24 05:25:19 +00:00
added readme for example
This commit is contained in:
136
example/README.md
Normal file
136
example/README.md
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
## Example
|
||||||
|
|
||||||
|
|
||||||
|
pubspec.yaml:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
name: example
|
||||||
|
version: 1.0.0
|
||||||
|
|
||||||
|
environment:
|
||||||
|
sdk: ">=2.12.0 <3.0.0"
|
||||||
|
|
||||||
|
|
||||||
|
dependencies:
|
||||||
|
cherrypick:
|
||||||
|
path: ../
|
||||||
|
|
||||||
|
dev_dependencies:
|
||||||
|
test: ^1.16.8
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
main.dart:
|
||||||
|
|
||||||
|
|
||||||
|
```dart
|
||||||
|
import 'dart:async';
|
||||||
|
import 'package:meta/meta.dart';
|
||||||
|
import 'package:cherrypick/scope.dart';
|
||||||
|
import 'package:cherrypick/module.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 {
|
||||||
|
bool isMock;
|
||||||
|
|
||||||
|
FeatureModule({required this.isMock});
|
||||||
|
|
||||||
|
@override
|
||||||
|
void builder(Scope currentScope) {
|
||||||
|
bind<DataRepository>()
|
||||||
|
.withName("networkRepo")
|
||||||
|
.toProvide(
|
||||||
|
() => NetworkDataRepository(
|
||||||
|
currentScope.resolve<ApiClient>(
|
||||||
|
named: isMock ? "apiClientMock" : "apiClientImpl",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.singeltone();
|
||||||
|
bind<DataBloc>().toProvide(
|
||||||
|
() => DataBloc(
|
||||||
|
currentScope.resolve<DataRepository>(named: "networkRepo"),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() async {
|
||||||
|
final scope = openRootScope().installModules([
|
||||||
|
AppModule(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
final subScope = scope
|
||||||
|
.openSubScope("featureScope")
|
||||||
|
.installModules([FeatureModule(isMock: true)]);
|
||||||
|
|
||||||
|
final dataBloc = subScope.resolve<DataBloc>();
|
||||||
|
dataBloc.data.listen((d) => print('Received data: $d'),
|
||||||
|
onError: (e) => print('Error: $e'), onDone: () => print('DONE'));
|
||||||
|
|
||||||
|
await dataBloc.fetchData();
|
||||||
|
}
|
||||||
|
|
||||||
|
class DataBloc {
|
||||||
|
final DataRepository _dataRepository;
|
||||||
|
|
||||||
|
Stream<String> get data => _dataController.stream;
|
||||||
|
StreamController<String> _dataController = new StreamController.broadcast();
|
||||||
|
|
||||||
|
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({@required String url, String token, Map requestBody});
|
||||||
|
}
|
||||||
|
|
||||||
|
class ApiClientMock implements ApiClient {
|
||||||
|
@override
|
||||||
|
Future sendRequest(
|
||||||
|
{@required String? url, String? token, Map? requestBody}) async {
|
||||||
|
return 'Local Data';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ApiClientImpl implements ApiClient {
|
||||||
|
@override
|
||||||
|
Future sendRequest(
|
||||||
|
{@required String? url, String? token, Map? requestBody}) async {
|
||||||
|
return 'Network data';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user