mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-24 05:25:19 +00:00
fixed example
This commit is contained in:
@@ -4,15 +4,25 @@ import 'package:dart_di/experimental/scope.dart';
|
|||||||
import 'package:dart_di/experimental/module.dart';
|
import 'package:dart_di/experimental/module.dart';
|
||||||
|
|
||||||
class AppModule extends Module {
|
class AppModule extends Module {
|
||||||
|
bool isMock;
|
||||||
|
|
||||||
|
AppModule({required this.isMock});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void builder(Scope currentScope) {
|
void builder(Scope currentScope) {
|
||||||
bind<ApiClient>().withName("apiClient").toInstance(ApiClientMock());
|
bind<ApiClient>().withName("apiClientMock").toInstance(ApiClientMock());
|
||||||
bind<DataRepository>().withName("networkRepo").toProvide(
|
bind<ApiClient>().withName("apiClientImpl").toInstance(ApiClientImpl());
|
||||||
|
|
||||||
|
bind<DataRepository>()
|
||||||
|
.withName("networkRepo")
|
||||||
|
.toProvide(
|
||||||
() => NetworkDataRepository(
|
() => NetworkDataRepository(
|
||||||
currentScope.resolve<ApiClient>(named: "apiClient"),
|
currentScope.resolve<ApiClient>(
|
||||||
|
named: isMock ? "apiClientMock" : "apiClientImpl",
|
||||||
),
|
),
|
||||||
);
|
),
|
||||||
// .singeltone();
|
)
|
||||||
|
.singeltone();
|
||||||
bind<DataBloc>().toProvide(
|
bind<DataBloc>().toProvide(
|
||||||
() => DataBloc(
|
() => DataBloc(
|
||||||
currentScope.resolve<DataRepository>(named: "networkRepo"),
|
currentScope.resolve<DataRepository>(named: "networkRepo"),
|
||||||
@@ -23,7 +33,7 @@ class AppModule extends Module {
|
|||||||
|
|
||||||
void main() async {
|
void main() async {
|
||||||
final scope = openRootScope().installModules([
|
final scope = openRootScope().installModules([
|
||||||
AppModule(),
|
AppModule(isMock: false),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
final dataBloc = scope.resolve<DataBloc>();
|
final dataBloc = scope.resolve<DataBloc>();
|
||||||
@@ -77,6 +87,14 @@ class ApiClientMock implements ApiClient {
|
|||||||
@override
|
@override
|
||||||
Future sendRequest(
|
Future sendRequest(
|
||||||
{@required String? url, String? token, Map? requestBody}) async {
|
{@required String? url, String? token, Map? requestBody}) async {
|
||||||
return 'hello world';
|
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