flutterdartdependency-injectionblocget-it

GetIt package - Object/factory not registered inside GetIt


I am using the GetIt package in my Flutter project to manage dependencies. However, I'm facing an issue where the package is throwing an _AssertionError with the following message:

'package:get_it/get_it_impl.dart': Failed assertion: line 372 pos 7: 'instanceFactory != null': Object/factory with type Client is not registered inside GetIt. (Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance; Did you forget to register it?)

I have tried to await the initialization of the dependencies inside the main function before running the app, but the error persists. I have no clue how to debug this issue.

Can anyone please guide me on how to resolve this error and properly register my dependencies with GetIt? Any help would be appreciated. Thank you.

dependencies:

bloc: ^8.1.1
flutter_bloc: ^8.1.2
get_it: ^7.2.0

code:

//main.dart
import 'package:bloc_app/theme.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'home_feature/home_screen.dart';
import 'home_feature/application/bloc/api_request_bloc.dart';

import 'package:bloc_app/injection.dart' as di;

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: AppTheme.ligthTheme,
      darkTheme: AppTheme.darkTheme,
      themeMode: ThemeMode.system,
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      home: BlocProvider(
        create: (context) => di.sl<ApiRequestBloc>(),
        child: const MyHomePage(title: 'Bloc App'),
      ),
    );
  }
}

The Get_it instance is created globaly here.

//injection.dart
import 'package:bloc_app/home_feature/application/bloc/api_request_bloc.dart';
import 'package:bloc_app/home_feature/domain/repositories/advicer_repository.dart';
import 'package:bloc_app/home_feature/domain/usecases/advicer_usecases.dart';
import 'package:bloc_app/home_feature/infrastructure/datasources/advicer_remote_datasource.dart';
import 'package:bloc_app/home_feature/infrastructure/repositories/advicer_repository_impl.dart';
import 'package:get_it/get_it.dart';

import 'package:http/http.dart' as http;

final sl = GetIt.instance;

Future<void> init() async {
  // Blocs
  sl.registerFactory(() => ApiRequestBloc(usecases: sl()));

  //Usecases
  sl.registerLazySingleton(() => AdvicerUseCases(advicerRepository: sl()));

  //Repositories
  sl.registerLazySingleton<AdvicerRepository>(
      () => AdvicerRepositoryImpl(advicerRemoteDataSource: sl()));

  //Datasources
  sl.registerLazySingleton<AdvicerRemoteDataSource>(
      () => AdvicerRemoteDataSourceImpl(client: sl()));

  //Extern
  sl.registerLazySingleton(() => http.Client);
}

The used bloc that get used inside the main.dart

//api_request_bloc.dart
import 'package:bloc/bloc.dart';
import 'package:bloc_app/home_feature/domain/entities/advice_entity.dart';
import 'package:bloc_app/home_feature/domain/failures/failures.dart';
import 'package:bloc_app/home_feature/domain/usecases/advicer_usecases.dart';
import 'package:dartz/dartz.dart';
// ignore: depend_on_referenced_packages
import 'package:meta/meta.dart';

part './api_request_event.dart';
part './api_request_state.dart';

class ApiRequestBloc extends Bloc<ApiRequestEvent, ApiRequestState> {
  final AdvicerUseCases usecases;

  ApiRequestBloc({required this.usecases}) : super(ApiRequestInitial()) {
    on<ApiRequestEvent>((event, emit) async {
      emit(ApiRequestLoading());

      Either<AdviceEntity, Failure> adviceOrFailure =
          await usecases.getAdviceUsecase();

      //If usecase gives error than state retunres falure otherwise the advice get shown
      adviceOrFailure.fold(
        (advice) => emit(ApiRequestLoaded(advice: advice.advice)),
        (failure) => emit(ApiRequestError(
          error: _mapFailureToError(failure),
        )),
      );
    });
  }

  String _mapFailureToError(Failure failure) {
    switch (failure.runtimeType) {
      case ServerFailure:
        return 'Error: ${failure.runtimeType} ~ could not communicate with the server.';
      case GeneralFailure:
        return 'Error: ${failure.runtimeType} ~ Could not define error.';

      default:
        return 'Error: ${failure.runtimeType} ~ Could not define error.';
    }
  }
}

Solution

  • You are missing () while registering http.Client in file injection.dart

      //Extern
      sl.registerLazySingleton(() => http.Client());