Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 44 additions & 13 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,56 @@ on:
branches: [master]

jobs:
test:
format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: subosito/flutter-action@v2
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup Flutter
uses: subosito/flutter-action@v2
with:
channel: stable
cache: true

- name: Install dependencies
run: flutter pub get

- name: Check code formatting
run: dart format --set-exit-if-changed .

analyze:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup Flutter
uses: subosito/flutter-action@v2
with:
channel: stable
cache: true

- name: Install dependencies
run: flutter pub get

- name: Run linter
run: flutter analyze
- name: Run tests
run: flutter test
run: dart analyze --fatal-infos .

format:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: subosito/flutter-action@v2
- run: dart format .
- uses: stefanzweifel/git-auto-commit-action@v4
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup Flutter
uses: subosito/flutter-action@v2
with:
commit_message: Update formatted files
file_pattern: "*.dart"
channel: stable
cache: true

- name: Install dependencies
run: flutter pub get

- name: Run tests
run: flutter test
4 changes: 2 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class _MyHomePageState extends State<MyHomePage> {
// Access the instance of the registered AppModel
// As we don't know for sure if AppModel is already ready we use isReady
getIt.isReady<AppModel>().then(
(_) => getIt<AppModel>().addListener(update),
);
(_) => getIt<AppModel>().addListener(update),
);
// Alternative
// getIt.getAsync<AppModel>().addListener(update);

Expand Down
4 changes: 3 additions & 1 deletion lib/get_it.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ typedef ScopeDisposeFunc = FutureOr Function();
/// For async Factories that expect up to two parameters if you need only one use `void` for the one
/// you don't use
typedef FactoryFuncParamAsync<T, P1, P2> = Future<T> Function(
P1 param1, P2 param2);
P1 param1,
P2 param2,
);

/// Data structure used to identify a dependency by type and instanceName
class InitDependency implements Type {
Expand Down
7 changes: 4 additions & 3 deletions test/async_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ void main() {

expect(constructorCounter, 0);

final Iterable<TestBaseClass> instances = await getIt
.getAllAsync<TestBaseClass>();
final Iterable<TestBaseClass> instances =
await getIt.getAllAsync<TestBaseClass>();

expect(instances.length, 2);
expect(instances.first is TestClass, true);
Expand All @@ -188,7 +188,8 @@ void main() {
getIt.allowRegisterMultipleImplementationsOfoneType = false;
});

test('signalReady will throw if any Singletons that has signalsReady==true '
test(
'signalReady will throw if any Singletons that has signalsReady==true '
'have not signaled completion', () async {
final getIt = GetIt.instance;

Expand Down
32 changes: 19 additions & 13 deletions test/get_it_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1173,12 +1173,15 @@ void main() {

expect(instance1 is TestClass, true);

expect(() {
getIt.changeTypeInstanceName<TestClass>(
instanceName: 'instanceName',
newInstanceName: 'instanceNameExisting',
);
}, throwsStateError);
expect(
() {
getIt.changeTypeInstanceName<TestClass>(
instanceName: 'instanceName',
newInstanceName: 'instanceNameExisting',
);
},
throwsStateError,
);
});

test('change registration name of instance', () async {
Expand Down Expand Up @@ -1242,13 +1245,16 @@ void main() {

getIt.registerSingleton(TestClass(), instanceName: 'instanceName');

expect(() {
// ignore: unused_local_variable
final TestClass2 instance1 = getIt.get(
type: TestClass,
instanceName: 'instanceName',
);
}, throwsA(isA<AssertionError>()));
expect(
() {
// ignore: unused_local_variable
final TestClass2 instance1 = getIt.get(
type: TestClass,
instanceName: 'instanceName',
);
},
throwsA(isA<AssertionError>()),
);
},
);

Expand Down
74 changes: 46 additions & 28 deletions test/scope_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,13 @@ void main() {
Object? shadowingObject;

getIt.registerSingleton<TestClassShadowChangHandler>(
TestClassShadowChangHandler((shadowState, shadow) {
isShadowed = shadowState;
shadowingObject = shadow;
}, 'Basescope'),
TestClassShadowChangHandler(
(shadowState, shadow) {
isShadowed = shadowState;
shadowingObject = shadow;
},
'Basescope',
),
);

getIt.pushNewScope();
Expand Down Expand Up @@ -146,10 +149,13 @@ void main() {
Object? shadowingObject;

getIt.registerSingleton<TestClassShadowChangHandler>(
TestClassShadowChangHandler((shadowState, shadow) {
isShadowed = shadowState;
shadowingObject = shadow;
}, 'Basescope'),
TestClassShadowChangHandler(
(shadowState, shadow) {
isShadowed = shadowState;
shadowingObject = shadow;
},
'Basescope',
),
);

getIt.pushNewScope();
Expand Down Expand Up @@ -183,10 +189,13 @@ void main() {
getIt.onScopeChanged = (pushed) => scopeChanged++;

getIt.registerLazySingleton<TestBaseClass>(
() => TestClassShadowChangHandler((shadowState, shadow) {
isShadowed = shadowState;
shadowingObject = shadow;
}, 'Basescope'),
() => TestClassShadowChangHandler(
(shadowState, shadow) {
isShadowed = shadowState;
shadowingObject = shadow;
},
'Basescope',
),
);

getIt.pushNewScope();
Expand Down Expand Up @@ -238,10 +247,13 @@ void main() {
Object? shadowingObject;

getIt.registerSingleton<TestBaseClass>(
TestClassShadowChangHandler((shadowState, shadow) {
isShadowed = shadowState;
shadowingObject = shadow;
}, 'Basescope'),
TestClassShadowChangHandler(
(shadowState, shadow) {
isShadowed = shadowState;
shadowingObject = shadow;
},
'Basescope',
),
);

getIt.pushNewScope();
Expand Down Expand Up @@ -290,23 +302,29 @@ void main() {
return newInstance;
});
getIt.registerSingleton<TestBaseClass>(
TestClassShadowChangHandler((shadowState, shadow) {
isShadowed = shadowState;
shadowingObject = shadow;
}, '2, Scope'),
TestClassShadowChangHandler(
(shadowState, shadow) {
isShadowed = shadowState;
shadowingObject = shadow;
},
'2, Scope',
),
);

getIt.pushNewScope();

Object? shadowingInstance;
getIt.registerSingletonWithDependencies<TestBaseClass>(() {
final newInstance = TestClassShadowChangHandler(
(shadowState, shadow) {},
'2, Scope',
);
shadowingInstance = newInstance;
return newInstance;
}, dependsOn: [TestClass]);
getIt.registerSingletonWithDependencies<TestBaseClass>(
() {
final newInstance = TestClassShadowChangHandler(
(shadowState, shadow) {},
'2, Scope',
);
shadowingInstance = newInstance;
return newInstance;
},
dependsOn: [TestClass],
);

/// The instance is not created yet because the async init function hasn't completed
expect(isShadowed, false);
Expand Down