📒
Mobile Development Reference-TCE-C01
  • Welcome
  • Introduction
    • Project Description
  • First Steps
    • Creating Project
    • Setting up Workspace
      • Project structure
      • Setting up the app
  • Routing
    • Setting up auto route
    • Creating the first screen
    • Setting up router
    • Adding router to Material App
  • Products Feature
    • Creating models
      • Introduction to freezed
      • Introduction to JSON annotations
      • Creating products model
    • Creating products cubit
      • Creating Products States
      • Creating Products Cubit
      • Implementing Get Products Functionality
    • Creating products repository
      • Setting up Dio
      • Making your first http request with DIO
    • Implementing Products Screen UI
      • Adding Products Grid
      • Adding Products Tile
      • Add Products Error Widget
    • Consuming the products cubit
      • Setting up Dependency injection
      • Creating Bloc Provider for ProductsCubit
      • Mapping Products Cubit states to UI
  • Testing
    • Widget Testing
    • Unit Testing
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. Products Feature
  2. Consuming the products cubit

Setting up Dependency injection

Installing get_it package

Add this to your package's pubspec.yaml file:

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()));
}
PreviousConsuming the products cubitNextCreating Bloc Provider for ProductsCubit

Last updated 4 years ago

Was this helpful?