Skip to content

Commit 9d1a317

Browse files
refactor: variable name change and updated readme
1 parent c8b4496 commit 9d1a317

File tree

5 files changed

+26
-17
lines changed

5 files changed

+26
-17
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,12 @@ You have to pass a factory function `func` that returns an instance of an implem
154154
### Overwriting registrations
155155

156156
If you try to register a type more than once you will fail with an assertion in debug mode because normally this is not needed and probably a bug.
157-
If you really have to overwrite a registration, then you can by setting the property `allowReassignment==true`.
157+
If you really have to overwrite a registration, then you can by setting the property `allowReassignment = true`.
158+
159+
### Skip Double registrations
160+
161+
If you try to register a type more than once and when `allowReassignment = false` you will fail with an assertion in debug mode.
162+
If you want to just skip this double registration silently without an error, then you can by setting the property `skipDoubleRegistration = true`.
158163

159164
### Testing if a Singleton is already registered
160165

lib/get_it.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'dart:collection';
77

88
import 'package:async/async.dart';
99
import 'package:collection/collection.dart' show IterableExtension;
10+
import 'package:meta/meta.dart';
1011

1112
part 'get_it_impl.dart';
1213

@@ -152,8 +153,9 @@ abstract class GetIt {
152153
bool allowReassignment = false;
153154

154155
/// By default it's throws error when [allowReassignment]= false. and trying to register same type
155-
/// If you really need, you can disable the Asserts / Errror by setting[ignoreReassignmentError]= true
156-
bool ignoreReassignmentError = false;
156+
/// If you really need, you can disable the Asserts / Errror by setting[skipDoubleRegistration]= true
157+
@visibleForTesting
158+
bool skipDoubleRegistration = false;
157159

158160
/// Till V7.6.7 GetIt didn't allow to register multiple instances of the same type.
159161
/// if you want to register multiple instances of the same type you can enable this

lib/get_it_impl.dart

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,10 @@ class _GetItImplementation implements GetIt {
376376
bool allowReassignment = false;
377377

378378
/// By default it's throws error when [allowReassignment]= false. and trying to register same type
379-
/// If you really need, you can disable the Asserts / Errror by setting[ignoreReassignmentError]= true
379+
/// If you really need, you can disable the Asserts / Errror by setting[skipDoubleRegistration]= true
380+
@visibleForTesting
380381
@override
381-
bool ignoreReassignmentError = false;
382+
bool skipDoubleRegistration = false;
382383

383384
/// Is used by several other functions to retrieve the correct [_ServiceFactory]
384385
_ServiceFactory<T, dynamic, dynamic>?
@@ -1033,28 +1034,28 @@ class _GetItImplementation implements GetIt {
10331034
throwIf(
10341035
existingTypeRegistration.namedFactories.containsKey(instanceName) &&
10351036
!allowReassignment &&
1036-
!ignoreReassignmentError,
1037+
!skipDoubleRegistration,
10371038
ArgumentError(
10381039
'Object/factory with name $instanceName and '
10391040
'type $T is already registered inside GetIt. ',
10401041
),
10411042
);
10421043

1043-
/// skip registration
1044-
if (ignoreReassignmentError && !allowReassignment) {
1044+
/// skip double registration
1045+
if (skipDoubleRegistration && !allowReassignment) {
10451046
return;
10461047
}
10471048
} else {
10481049
if (existingTypeRegistration.factories.isNotEmpty) {
10491050
throwIfNot(
10501051
allowReassignment ||
10511052
GetIt.allowRegisterMultipleImplementationsOfoneType ||
1052-
ignoreReassignmentError,
1053+
skipDoubleRegistration,
10531054
ArgumentError('Type $T is already registered inside GetIt. '),
10541055
);
10551056

1056-
/// skip registration
1057-
if (ignoreReassignmentError && !allowReassignment) {
1057+
/// skip double registration
1058+
if (skipDoubleRegistration && !allowReassignment) {
10581059
return;
10591060
}
10601061
}

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ environment:
1010
dependencies:
1111
async: ^2.11.0
1212
collection: ^1.17.1
13+
meta: ^1.12.0
1314

1415
dev_dependencies:
1516
lint: ^2.1.2

test/ignore_error_test.dart renamed to test/skip_double_registration_test.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ void main() {
55
test(' Throws ArgumentError ', () async {
66
final getIt = GetIt.instance;
77
getIt.allowReassignment = false;
8-
getIt.ignoreReassignmentError = false;
8+
getIt.skipDoubleRegistration = false;
99
getIt.reset();
1010
getIt.registerSingleton<DataStore>(MockDataStore());
1111

@@ -19,30 +19,30 @@ void main() {
1919
final getIt = GetIt.instance;
2020
getIt.reset();
2121
getIt.allowReassignment = true;
22-
getIt.ignoreReassignmentError = false;
22+
getIt.skipDoubleRegistration = false;
2323
getIt.registerSingleton<DataStore>(MockDataStore());
2424
getIt.registerSingleton<DataStore>(RemoteDataStore());
2525

2626
expect(getIt<DataStore>(), isA<RemoteDataStore>());
2727
});
2828

29-
test(' Ignores ReassignmentError ', () async {
29+
test(' Ignores Double registration error ', () async {
3030
final getIt = GetIt.instance;
3131
getIt.reset();
3232
getIt.allowReassignment = false;
33-
getIt.ignoreReassignmentError = true;
33+
getIt.skipDoubleRegistration = true;
3434
getIt.registerSingleton<DataStore>(MockDataStore());
3535
final remoteDataStore = RemoteDataStore();
3636
getIt.registerSingleton<DataStore>(remoteDataStore);
3737

3838
expect(getIt<DataStore>(), isA<MockDataStore>());
3939
});
4040

41-
test(' does not care about [ignoreReassignmentError] varibale ', () async {
41+
test(' does not care about [skipDoubleRegistration] varibale ', () async {
4242
final getIt = GetIt.instance;
4343
getIt.reset();
4444
getIt.allowReassignment = true;
45-
getIt.ignoreReassignmentError = true;
45+
getIt.skipDoubleRegistration = true;
4646
getIt.registerSingleton<DataStore>(MockDataStore());
4747
getIt.registerSingleton<DataStore>(RemoteDataStore());
4848

0 commit comments

Comments
 (0)