diff --git a/kiwi/lib/src/kiwi_container.dart b/kiwi/lib/src/kiwi_container.dart index dceb112..2f0de40 100644 --- a/kiwi/lib/src/kiwi_container.dart +++ b/kiwi/lib/src/kiwi_container.dart @@ -8,8 +8,19 @@ typedef T Factory(KiwiContainer container); /// A simple service container. class KiwiContainer { /// Creates a scoped container. - KiwiContainer.scoped() - : _namedProviders = Map>>(); + /// + /// If [parent] is set, the new scoped instance will include its providers. + KiwiContainer.scoped({ + KiwiContainer? parent, + }) : _namedProviders = >>{ + if (parent != null) + ...parent._namedProviders.map( + // [Map.from] is needed to create a copy of value and not use its reference, + // because if only value is passed, everything included in the parent will be + // added to the new instance at any time, even after this scoped instance has been created. + (key, value) => MapEntry(key, Map.from(value)), + ), + }; static final KiwiContainer _instance = KiwiContainer.scoped();