Skip to content
Back to skills

Flutter Dependency Injection

ASecurity

The simplest approach is to replace the `AuthRepository` registration in `GetIt` before you pump the widget, then reset the locator after the test. That matches the skill’s test-mock swap pattern. If production code looks like this: ```dart abstract class IAuthRepository { Future<bool> isLoggedIn(); } @LazySingleton(as: IAuthRepository) class AuthRepository implements IAuthRepository { @override Future<bool> isLoggedIn() async => true; } ``` Then your widget test can override it with a mock: ...

  • 584 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added September 5, 2026
development

Security analysis

A100/100

Pro scans all 12 files and shows the line behind each finding

Scanned September 5, 2026

npx -y skills add HoangNguyen0403/agent-skills-standard --skill flutter-dependency-injection --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Flutter Dependency Injection?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Flutter Dependency Injection
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/hoangnguyen0403-flutter-dependency-injection-f640ea7b/badge)](https://www.skillsdirectory.com/skills/hoangnguyen0403-flutter-dependency-injection-f640ea7b)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
The simplest approach is to replace the `AuthRepository` registration in `GetIt` before you pump the widget, then reset the locator after the test. That matches the skill’s test-mock swap pattern.

If production code looks like this:

```dart
abstract class IAuthRepository {
  Future<bool> isLoggedIn();
}

@LazySingleton(as: IAuthRepository)
class AuthRepository implements IAuthRepository {
  @override
  Future<bool> isLoggedIn() async => true;
}
```

Then your widget test can override it with a mock:

```dart
import 'package:flutter_test/flutter_test.dart';
import 'package:get_it/get_it.dart';
import 'package:mocktail/mocktail.dart';

class MockAuthRepository extends Mock implements IAuthRepository {}

final getIt = GetIt.instance;

void main() {
  late MockAuthRepository mockAuthRepository;

  setUp(() async {
    mockAuthRepository = MockAuthRepository();

    if (getIt.isRegistered<IAuthRepository>()) {
      getIt.unregister<IAuthRepository>();
    }

    getIt.registerLazySingleton<IAuthRepository>(
      () => mockAuthRepository,
    );
  });

  tearDown(() async {
    await getIt.reset();
  });

  testWidgets('shows signed-in state', (tester) async {
    when(() => mockAuthRepository.isLoggedIn())
        .thenAnswer((_) async => true);

    await tester.pumpWidget(const MyApp());
    await tester.pumpAndSettle();

    expect(find.text('Welcome'), findsOneWidget);
  });
}
```

The important parts are:

- override the abstract type (`IAuthRepository`), not the concrete class
- register the mock before building the widget
- clean up with `getIt.reset()` so one test does not leak into the next

This follows the skill’s preferred DI style:

- inject abstractions, not concrete implementations
- keep production registration annotation-driven
- use targeted test overrides when you need mocks

If your test needs the normal graph plus one replacement, the pattern is:

```dart
await getIt.reset();
await configureDependencies(); // or your normal setup
getIt.unregister<IAuthRepository>();
getIt.registerLazySingleton<IAuthRepository>(() => mockAuthRepository);
```

That lets the rest of the app resolve normally while `IAuthRepository` is swapped for the mock.

One last rule: avoid calling `getIt` from widget `build()` methods just to fetch dependencies on demand. Constructor injection plus provider wiring makes test overrides much easier and keeps the widget tree predictable.

Files in this skill

  • eval-1.baseline.md2.1 KB
  • eval-1.with-skill.md2.4 KB
  • eval-2.baseline.md2 KB
  • eval-2.with-skill.md1.8 KB
  • eval-3.baseline.md2.5 KB
  • eval-3.with-skill.md2.4 KB
  • trigger-1.md147 B
  • trigger-2.md138 B
  • trigger-3.md161 B
  • trigger-4.md154 B
  • trigger-5.md138 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.