Skip to content

Commit

Permalink
feat: add support to dependency injection using get it and injectable
Browse files Browse the repository at this point in the history
  • Loading branch information
brunogabriel committed Oct 17, 2024
1 parent 5a950ac commit 3beae4d
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 1 deletion.
37 changes: 37 additions & 0 deletions pokedex/lib/app/di/injection.config.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pokedex/lib/app/di/injection.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import 'package:get_it/get_it.dart';
import 'package:injectable/injectable.dart';
import 'injection.config.dart';

@InjectableInit(asExtension: false)
Future<void> configureDependencies() async => init(GetIt.instance);
5 changes: 4 additions & 1 deletion pokedex/lib/main.dart → pokedex/lib/app/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'package:flutter/material.dart';
import 'package:pokedex/app/di/injection.dart';

void main() {
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await configureDependencies();
runApp(const MyApp());
}

Expand Down
17 changes: 17 additions & 0 deletions pokedex/lib/common/network/di/network_module.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:dio/dio.dart';
import 'package:injectable/injectable.dart';

@module
abstract class NetworkModule {
@Named('baseUrl')
@injectable
String get baseUrl => 'https://pokeapi.co/api/v2/';

@singleton
Dio provideClient(
@Named('baseUrl') String baseUrl,
) =>
Dio(
BaseOptions(baseUrl: baseUrl),
);
}
1 change: 1 addition & 0 deletions pokedex/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies:
flutter_svg: ^2.0.10+1
shared_preferences: ^2.3.2
collection: ^1.18.0
dio: ^5.7.0

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 3beae4d

Please sign in to comment.