Skip to content

Commit

Permalink
fix error with two WidgetModules with the same type
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderShirokih committed Jul 26, 2021
1 parent 3758e51 commit 0d89c5c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class ModularInterface {
@visibleForTesting
void overrideBinds(List<Bind> binds);
void init(Module module);
void bindModule(Module module, [String path]);
void bindModule(Module module, {String path, bool rebindDuplicates});
void debugPrintModular(String text);
T bind<T extends Object>(Bind<T> bind);
Future<void> isModuleReady<M>();
Expand Down
45 changes: 36 additions & 9 deletions flutter_modular/lib/src/presenters/modular_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,41 @@ class ModularImpl implements ModularInterface {
}

@override
void bindModule(Module module, [String path = '']) {
void bindModule(Module module,
{String path = '', bool rebindDuplicates = false}) {
final name = module.runtimeType.toString();
if (!injectMap.containsKey(name)) {
module.paths.add(path);
injectMap[name] = module;
module.instance(_getAllSingletons());
debugPrintModular("-- ${module.runtimeType.toString()} INITIALIZED");
} else {
// The same module with default path, so rebind the module
if (path.isEmpty && rebindDuplicates) {
final oldModule = injectMap[name];

// Remove old injects
oldModule?.cleanInjects();
injectMap.remove(name);

// Install new injects
bindModule(module, path: name, rebindDuplicates: false);
return;
}

// Add the new path only if the last path in paths list is different from the current one
final _paths = injectMap[name]?.paths;
if (_paths?.isNotEmpty == true && _paths?.last != path) _paths?.add(path);

if (_paths?.isNotEmpty == true && _paths?.last != path) {
_paths?.add(path);
}
}
}

@override
void init(Module module) {
_initialModule = module;
bindModule(module, module.runtimeType.toString());
bindModule(module, path: module.runtimeType.toString());
}

@override
Expand Down Expand Up @@ -105,14 +122,22 @@ class ModularImpl implements ModularInterface {
B get<B extends Object>({List<Type>? typesInRequestList, B? defaultValue}) {
var typesInRequest = typesInRequestList ?? [];
if (Modular.flags.experimentalNotAllowedParentBinds) {
final module = routerDelegate.currentConfiguration?.currentModule?.runtimeType.toString() ?? 'AppModule';
var bind = injectMap[module]!.binds.firstWhere((b) => b.inject is B Function(Inject), orElse: () => BindEmpty());
final module = routerDelegate
.currentConfiguration?.currentModule?.runtimeType
.toString() ??
'AppModule';
var bind = injectMap[module]!.binds.firstWhere(
(b) => b.inject is B Function(Inject),
orElse: () => BindEmpty());
if (bind is BindEmpty) {
bind = injectMap[module]!.binds.firstWhere((b) => b.inject is Future<B> Function(Inject), orElse: () => BindEmpty());
bind = injectMap[module]!.binds.firstWhere(
(b) => b.inject is Future<B> Function(Inject),
orElse: () => BindEmpty());
}

if (bind is BindEmpty) {
throw ModularError('\"${B.toString()}\" not found in \"$module\" module');
throw ModularError(
'\"${B.toString()}\" not found in \"$module\" module');
}
}
var result = _findExistingInstance<B>();
Expand All @@ -122,7 +147,8 @@ class ModularImpl implements ModularInterface {
}

for (var key in injectMap.keys) {
final value = _getInjectableObject<B>(key, typesInRequestList: typesInRequest, checkKey: false);
final value = _getInjectableObject<B>(key,
typesInRequestList: typesInRequest, checkKey: false);
if (value != null) {
return value;
}
Expand Down Expand Up @@ -187,7 +213,8 @@ class ModularImpl implements ModularInterface {
}

@override
T bind<T extends Object>(Bind<T> bind) => Inject(overrideBinds: _overrideBinds ?? []).get(bind);
T bind<T extends Object>(Bind<T> bind) =>
Inject(overrideBinds: _overrideBinds ?? []).get(bind);

@override
Future<void> isModuleReady<M>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class ModularRouteInformationParser extends RouteInformationParser<ModularRoute>
);
}
if (route.module != null) {
Modular.bindModule(route.module!, uri.path);
Modular.bindModule(route.module!, path: uri.path);
}
return router;
}
Expand All @@ -139,7 +139,7 @@ class ModularRouteInformationParser extends RouteInformationParser<ModularRoute>
}

if (parseRoute.currentModule != null) {
Modular.bindModule(parseRoute.currentModule!, uri.path);
Modular.bindModule(parseRoute.currentModule!, path: uri.path);
}
return parseRoute;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class _ModularProviderState extends State<ModularProvider> {
@override
void initState() {
super.initState();
Modular.bindModule(widget.module);
Modular.bindModule(widget.module, rebindDuplicates: true);
}

@override
Expand Down

0 comments on commit 0d89c5c

Please sign in to comment.