Skip to content

Commit da310a0

Browse files
committed
update auto injector
1 parent a2670d9 commit da310a0

File tree

18 files changed

+90
-105
lines changed

18 files changed

+90
-105
lines changed

.vscode/settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
2-
"dart.flutterSdkPath": "/Users/jacobmoura/fvm/versions/stable",
2+
"dart.flutterSdkPath": "/Users/jacob/.puro/envs/stable/flutter",
33
"dart.lineLength": 80,
44
"editor.formatOnSave": true,
5+
"dart.sdkPath": "/Users/jacob/.puro/envs/stable/flutter/bin/cache/dart-sdk"
56
}

doc/docs/flutter_modular/dependency-injection.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ class AppModule extends Module {
114114
void binds(i) {
115115
i.add(XPTOEmail.new);
116116
i.add<EmailService>(XPTOEmailService.new);
117-
i.addSingleton(Client.new)
117+
i.addSingleton(Client.new);
118+
119+
// Register with Key
120+
i.addSingleton(Client.new, key: 'OtherClient');
118121
}
119122
120123
...
@@ -129,6 +132,12 @@ final client = Modular.get<Client>();
129132
130133
// or set a default value
131134
final client = Modular.get<Client>(defaultValue: Client());
135+
136+
// or use tryGet
137+
Client? client = Modular.tryGet<Client>();
138+
139+
// or get with key
140+
Client client = Modular.get(key: 'OtherCLient');
132141
```
133142

134143
## Auto Dispose

flutter_modular/CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## [6.1.0+1] - 2023-08-24
2+
3+
- Add: Register and get with keys:
4+
```dart
5+
i.add(MyController.new, key: 'Custom');
6+
...
7+
8+
Modular.get(key: 'Custom');
9+
```
10+
111
## [6.0.3] - 2023-08-09
212

313
- fix: [#875](https://github.com/Flutterando/modular/issues/875)

flutter_modular/lib/src/domain/services/bind_service.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'package:modular_core/modular_core.dart';
22
import 'package:result_dart/result_dart.dart';
33

44
abstract class BindService {
5-
Result<T, ModularError> getBind<T extends Object>();
6-
Result<bool, ModularError> disposeBind<T extends Object>();
7-
Result<Unit, ModularError> replaceInstance<T>(T instance, [Type? module]);
5+
Result<T, ModularError> getBind<T extends Object>([String? key]);
6+
Result<bool, ModularError> disposeBind<T extends Object>([String? key]);
7+
Result<Unit, ModularError> replaceInstance<T>(T instance, [String? key]);
88
}

flutter_modular/lib/src/domain/usecases/dispose_bind.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import 'package:result_dart/result_dart.dart';
44
import '../services/bind_service.dart';
55

66
abstract class DisposeBind {
7-
Result<bool, ModularError> call<T extends Object>();
7+
Result<bool, ModularError> call<T extends Object>([String? key]);
88
}
99

1010
class DisposeBindImpl implements DisposeBind {
@@ -13,7 +13,7 @@ class DisposeBindImpl implements DisposeBind {
1313
DisposeBindImpl(this.bindService);
1414

1515
@override
16-
Result<bool, ModularError> call<T extends Object>() {
17-
return bindService.disposeBind<T>();
16+
Result<bool, ModularError> call<T extends Object>([String? key]) {
17+
return bindService.disposeBind<T>(key);
1818
}
1919
}

flutter_modular/lib/src/domain/usecases/get_bind.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import 'package:result_dart/result_dart.dart';
44
import '../services/bind_service.dart';
55

66
abstract class GetBind {
7-
Result<T, ModularError> call<T extends Object>();
7+
Result<T, ModularError> call<T extends Object>([String? key]);
88
}
99

1010
class GetBindImpl implements GetBind {
@@ -13,7 +13,7 @@ class GetBindImpl implements GetBind {
1313
GetBindImpl(this.bindService);
1414

1515
@override
16-
Result<T, ModularError> call<T extends Object>() {
17-
return bindService.getBind<T>();
16+
Result<T, ModularError> call<T extends Object>([String? key]) {
17+
return bindService.getBind<T>(key);
1818
}
1919
}

flutter_modular/lib/src/domain/usecases/replace_instance.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import 'package:result_dart/result_dart.dart';
44
import '../services/bind_service.dart';
55

66
abstract class ReplaceInstance {
7-
Result<Unit, ModularError> call<T>(T instance, [Type? module]);
7+
Result<Unit, ModularError> call<T>(T instance, [String? key]);
88
}
99

1010
class ReplaceInstanceImpl implements ReplaceInstance {
@@ -13,7 +13,7 @@ class ReplaceInstanceImpl implements ReplaceInstance {
1313
ReplaceInstanceImpl(this.bindService);
1414

1515
@override
16-
Result<Unit, ModularError> call<T>(T instance, [Type? module]) {
17-
return bindService.replaceInstance<T>(instance, module);
16+
Result<Unit, ModularError> call<T>(T instance, [String? key]) {
17+
return bindService.replaceInstance<T>(instance, key);
1818
}
1919
}

flutter_modular/lib/src/infra/services/bind_service_impl.dart

+6-24
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,24 @@ class BindServiceImpl extends BindService {
1010
BindServiceImpl(this.injector);
1111

1212
@override
13-
Result<bool, ModularError> disposeBind<T extends Object>() {
14-
final result = injector.disposeSingleton<T>();
13+
Result<bool, ModularError> disposeBind<T extends Object>([String? key]) {
14+
final result = injector.disposeSingleton<T>(key: key);
1515
return Success(result != null);
1616
}
1717

1818
@override
19-
Result<T, ModularError> getBind<T extends Object>() {
19+
Result<T, ModularError> getBind<T extends Object>([String? key]) {
2020
try {
21-
final result = injector.get<T>();
21+
final result = injector.get<T>(key: key);
2222
return Success(result);
2323
} on AutoInjectorException catch (e, s) {
2424
return Failure(BindNotFoundException(e.toString(), s));
2525
}
2626
}
2727

2828
@override
29-
Result<Unit, ModularError> replaceInstance<T>(T instance, [Type? module]) {
30-
var tag = module?.toString() ?? '';
31-
32-
if (tag.isEmpty) {
33-
tag = injector.tags.firstWhere(
34-
(innerTag) => injector.isAdded<T>(innerTag),
35-
orElse: () => '',
36-
);
37-
} else {
38-
tag = injector.isAdded<T>(tag) ? tag : '';
39-
}
40-
41-
if (tag.isEmpty) {
42-
return BindNotFoundException(
43-
'$T unregistred',
44-
StackTrace.current,
45-
).toFailure();
46-
}
47-
48-
injector.replaceInstance<T>(instance, tag);
29+
Result<Unit, ModularError> replaceInstance<T>(T instance, [String? key]) {
30+
injector.replaceInstance(instance, key: key);
4931
return Success.unit();
5032
}
5133
}

flutter_modular/lib/src/presenter/modular_base.dart

+14-13
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ abstract class IModularBase {
4545
IModularNavigator? navigatorDelegate;
4646

4747
/// Request an instance by [Type]
48-
B get<B extends Object>();
48+
B get<B extends Object>({String? key});
4949

5050
/// Request an instance by [Type]
5151
/// <br>
5252
/// Return null if not found instance
53-
B? tryGet<B extends Object>();
53+
B? tryGet<B extends Object>({String? key});
5454

5555
/// Dispose a bind by [Type]
56-
bool dispose<B extends Object>();
56+
bool dispose<B extends Object>({String? key});
5757

5858
/// Navigator 2.0 initializator: RouteInformationParser
5959
ModularRouteInformationParser get routeInformationParser;
@@ -83,7 +83,7 @@ abstract class IModularBase {
8383
void unbindModule<T extends Module>({String? type});
8484

8585
/// replace instance
86-
void replaceInstance<T>(T instance, [Type? module]);
86+
void replaceInstance<T>(T instance, {String? key});
8787

8888
@visibleForTesting
8989
String get initialRoutePath;
@@ -132,17 +132,17 @@ class ModularBase implements IModularBase {
132132
});
133133

134134
@override
135-
bool dispose<B extends Object>() =>
136-
disposeBind<B>().getOrElse((left) => false);
135+
bool dispose<B extends Object>({String? key}) =>
136+
disposeBind<B>(key).getOrElse((left) => false);
137137

138138
@override
139-
B get<B extends Object>() {
140-
return getBind<B>().getOrThrow();
139+
B get<B extends Object>({String? key}) {
140+
return getBind<B>(key).getOrThrow();
141141
}
142142

143143
@override
144-
B? tryGet<B extends Object>() {
145-
return getBind<B>().getOrNull();
144+
B? tryGet<B extends Object>({String? key}) {
145+
return getBind<B>(key).getOrNull();
146146
}
147147

148148
@override
@@ -201,7 +201,8 @@ class ModularBase implements IModularBase {
201201
routerDelegate: routerDelegate,
202202
routeInformationParser: routeInformationParser,
203203
routeInformationProvider: PlatformRouteInformationProvider(
204-
initialRouteInformation: const RouteInformation(),
204+
// ignore: deprecated_member_use
205+
initialRouteInformation: const RouteInformation(location: '/'),
205206
),
206207
backButtonDispatcher: RootBackButtonDispatcher(),
207208
);
@@ -217,7 +218,7 @@ class ModularBase implements IModularBase {
217218
}
218219

219220
@override
220-
void replaceInstance<T>(T instance, [Type? module]) {
221-
replaceInstanceUsecase.call<T>(instance, module).getOrThrow();
221+
void replaceInstance<T>(T instance, {String? key}) {
222+
replaceInstanceUsecase.call<T>(instance, key).getOrThrow();
222223
}
223224
}

flutter_modular/lib/src/presenter/navigation/modular_route_information_parser.dart

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ignore_for_file: deprecated_member_use
2+
13
import 'dart:async';
24

35
import 'package:flutter/material.dart';
@@ -35,12 +37,16 @@ class ModularRouteInformationParser
3537
RouteInformation routeInformation) async {
3638
var path = '';
3739
if (!_firstParse) {
38-
if (routeInformation.location == null ||
39-
routeInformation.location == '/') {
40+
// 3.10 wrapper
41+
final location = [null].contains(routeInformation.location)
42+
? '/'
43+
: routeInformation.location;
44+
if (location == '/') {
4045
// ignore: invalid_use_of_visible_for_testing_member
4146
path = urlService.getPath() ?? Modular.initialRoutePath;
4247
} else {
43-
path = routeInformation.location!;
48+
// 3.10 wrapper
49+
path = [null].contains(location) ? '' : location;
4450
}
4551

4652
_firstParse = true;

flutter_modular/pubspec.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
name: flutter_modular
22
description: Smart project structure with dependency injection and route management
3-
version: 6.0.3
3+
version: 6.1.0+1
44
homepage: https://github.com/Flutterando/modular
55

66
environment:
77
sdk: ">=3.0.0 <4.0.0"
88

99
dependencies:
10-
modular_core: ">=3.0.3+1 <4.0.0"
10+
modular_core: ">=3.1.0 <4.0.0"
1111
meta: ">=1.3.0 <2.0.0"
1212
result_dart: ">=1.0.4 <2.0.0"
1313
flutter:

flutter_modular/test/src/infra/services/bind_service_impl_test.dart

+2-22
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void main() {
2525
});
2626
test('should throw error not found bind', () {
2727
when(() => injector.get<String>())
28-
.thenThrow(const AutoInjectorException('String'));
28+
.thenThrow(AutoInjectorException('String'));
2929
expect(service.getBind<String>().exceptionOrNull(),
3030
isA<BindNotFoundException>());
3131
});
@@ -41,32 +41,12 @@ void main() {
4141
group('replaceInstance', () {
4242
test('should replace instance returning unit', () {
4343
const instance = 'String';
44-
when(() => injector.tags).thenReturn({'String'});
45-
when(() => injector.isAdded<String>('String')).thenReturn(true);
44+
when(() => injector.isAdded<String>()).thenReturn(true);
4645
when(() => injector.replaceInstance<String>(instance)).thenReturnVoid();
4746

4847
final result = service.replaceInstance<String>(instance);
4948

5049
expect(result.isSuccess(), true);
5150
});
52-
53-
test('should return error if instance unregistred', () async {
54-
const instance = 'String';
55-
when(() => injector.isAdded<String>('String')).thenReturn(false);
56-
57-
final result = service.replaceInstance<String>(instance, String);
58-
59-
expect(result.isError(), true);
60-
});
61-
62-
test('should return error if instance unregistred without tags', () async {
63-
const instance = 'String';
64-
when(() => injector.tags).thenReturn({'String'});
65-
when(() => injector.isAdded<String>('String')).thenReturn(false);
66-
67-
final result = service.replaceInstance<String>(instance);
68-
69-
expect(result.isError(), true);
70-
});
7151
});
7252
}

flutter_modular/test/src/presenter/navigation/modular_route_information_parser_test.dart

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ignore_for_file: deprecated_member_use
2+
13
import 'dart:async';
24

35
import 'package:flutter/material.dart';

melos_modular.iml

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<module type="WEB_MODULE" version="4">
33
<component name="NewModuleRootManager" inherit-compiler-output="true">
4-
<exclude-output />
4+
<exclude-output/>
55
<content url="file://$MODULE_DIR$">
6-
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
6+
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false"/>
77
</content>
8-
<orderEntry type="sourceFolder" forTests="false" />
9-
<orderEntry type="library" name="Dart SDK" level="project" />
10-
<orderEntry type="library" name="Dart Packages" level="project" />
8+
<orderEntry type="sourceFolder" forTests="false"/>
9+
<orderEntry type="library" name="Dart SDK" level="project"/>
10+
<orderEntry type="library" name="Dart Packages" level="project"/>
11+
<orderEntry type="library" name="Flutter Plugins" level="project"/>
1112
</component>
12-
</module>
13+
</module>

modular.code-workspace

+1-6
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,5 @@
1616
"name": "Documentation",
1717
"path": "doc"
1818
}
19-
],
20-
"settings": {
21-
"dart.flutterSdkPath": "D:\\fvm\\sdks\\versions\\stable",
22-
"lcov.branchCoverage": "off",
23-
"lcov.path": "coverage/lcov.info"
24-
}
19+
]
2520
}

modular_core/lib/src/tracker.dart

+2-6
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,7 @@ class _Tracker implements Tracker {
173173
}
174174

175175
void _removeRegisters(String tag) {
176-
injector.disposeSingletonsByTag(
177-
tag,
178-
onRemoved: _disposeInstance,
179-
);
180-
injector.removeByTag(tag);
176+
injector.disposeInjectorByTag(tag, _disposeInstance);
181177

182178
printResolverFunc?.call('-- $tag DISPOSED');
183179
}
@@ -350,7 +346,7 @@ class _Tracker implements Tracker {
350346

351347
@override
352348
void finishApp() {
353-
injector.removeAll();
349+
injector.disposeRecursive();
354350
routeMap.clear();
355351
_disposeTags.clear();
356352
_nullableModule = null;

modular_core/pubspec.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
name: modular_core
22
description: Smart project structure with dependency injection and route management
3-
version: 3.0.3+1
3+
version: 3.1.0
44
homepage: https://github.com/Flutterando/modular
55

66
environment:
77
sdk: ">=3.0.0 <4.0.0"
88

99
dependencies:
10-
auto_injector: ">=1.2.0 <2.0.0"
10+
auto_injector: ">=2.0.0+2 <3.0.0"
1111
characters: ">=1.1.0 <2.0.0"
1212
meta: ">=1.3.0 <2.0.0"
1313

0 commit comments

Comments
 (0)