Installing get_it package
dependencies:
get_it: ^6.0.0
Now let's set up the dependency injection
First, create the "di.dart" file under src/di/ with the following code.
import 'package:dio/dio.dart';
import 'package:get_it/get_it.dart';
import 'package:techamp_flutter_shopping_app/app.dart';
GetIt getIt = GetIt.instance;
void registerDependencies() {
_registerConfigurations();
_registerProduct();
_registerProfile();
_registerCart();
}
void _registerCart() {
getIt.registerFactory<CartCubit>(() => CartCubit());
}
void _registerConfigurations() {
getIt.registerSingleton(
Dio(
BaseOptions(
baseUrl: 'https://fakestoreapi.com/',
connectTimeout: 20000,
receiveTimeout: 30000,
sendTimeout: 30000,
),
),
);
}
void _registerProduct() {
getIt
..registerFactory<ProductRepository>(() => ProductRepositoryImpl(getIt()))
..registerFactory(() => ProductsCubit(getIt()));
}
void _registerProfile() {
getIt
..registerFactory<ProfileRepository>(() => ProfileRepositoryImpl(getIt()))
..registerFactory(() => ProfileCubit(getIt()));
}