I am using GetIt (get_it: ^5.0.3) and have written a unit test to validate my usage. Each dependency is tested separately. All they do is test to see if the dependency is registered. The first three dependencies are registered. The remainder retrun false from the isRegestered function. The three successful ones are all testing dependencies that are imported from external packages. The remainder - the failing dependencies - are all classes developed as part of the project. Obviously this leads me to the conclusion that I am doing something fundamentally wrong. I know no other Flutter/Dart developers, so... Anyway, I am including the dependency injection/service locator file, the test thereof, and the code for the first failing class.
The dependency injection/service locator:
import 'package:get_it/get_it.dart';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import 'core/network/network_info.dart';
import 'core/presentation/converter/input_converter.dart';
import 'features/schedule/data/repositories/pill_box_set_repository_impl.dart';
import 'features/schedule/data/datasources/pill_box_set_local_data_source.dart';
import 'features/schedule/data/datasources/pill_box_set_remote_data_source.dart';
import 'features/schedule/domain/repositories/pill_box_set_repository.dart';
import 'features/schedule/domain/usecases/get_pill_box_set.dart';
import 'features/schedule/presentation/bloc/pill_box_set_bloc.dart';
final sl = GetIt.instance;
Future<void> init() async {
//! Features - Schedule
// Bloc
sl.registerFactory(() =>
PillBoxSetBloc(
pillBoxSetGetter: sl.get<GetPillBoxSet>(),
inputConverter: sl.get<InputConverter>(),
)
);
// Use cases
sl.registerLazySingleton(() => GetPillBoxSet(sl.get<PillBoxSetRepository>()));
// Repository
sl.registerLazySingleton<PillBoxSetRepository>(() =>
PillBoxSetRepositoryImpl(
networkInfo: sl.get<NetworkInfo>(),
localDataSource: sl.get<PillBoxSetLocalDataSource>(),
remoteDataSource: sl.get<PillBoxSetRemoteDataSource>(),
)
);
// Data sources
sl.registerLazySingleton<PillBoxSetLocalDataSource>(
() => PillBoxSetLocalDataSourceImpl(sharedPreferences: sl())
);
sl.registerLazySingleton<PillBoxSetRemoteDataSource>(
() => PillBoxSetRemoteDataSourceImpl(client: sl.get<http.Client>())
);
//! Core
sl.registerLazySingleton<InputConverter>(() => InputConverter());
sl.registerLazySingleton<NetworkInfo>(() =>
NetworkInfoImpl(sl.get<DataConnectionChecker>())
);
//! External
final sharedPreferences = await SharedPreferences.getInstance();
sl.registerLazySingleton<SharedPreferences>(() => sharedPreferences);
sl.registerLazySingleton<http.Client>(() => http.Client());
sl.registerLazySingleton<DataConnectionChecker>(() => DataConnectionChecker());
}
The test:
import 'package:data_connection_checker/data_connection_checker.dart';
import 'package:pusherman/core/presentation/converter/input_converter.dart';
import 'package:pusherman/features/schedule/data/datasources/pill_box_set_local_data_source.dart';
import 'package:pusherman/features/schedule/data/datasources/pill_box_set_remote_data_source.dart';
import 'package:pusherman/features/schedule/domain/repositories/pill_box_set_repository.dart';
import 'package:pusherman/features/schedule/domain/usecases/get_pill_box_set.dart';
import 'package:pusherman/features/schedule/presentation/bloc/pill_box_set_bloc.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:test/test.dart';
import 'package:http/http.dart' as http;
import 'package:pusherman/core/network/network_info.dart';
import '../lib/service_locator.dart' as di;
void main() async {
await di.init();
group('is registered', () {
test('DataConnectionChecker', () async {
expect(di.sl.isRegistered<DataConnectionChecker>(), isTrue);
});
test('http.Client', () {
expect(di.sl.isRegistered<http.Client>(), isTrue);
});
test('SharedPreferences', () {
expect(di.sl.isRegistered<SharedPreferences>(), isTrue);
});
test('NetworkInfo', () async {
expect(di.sl.isRegistered<NetworkInfo>(), isTrue);
});
test('InputConverter', () {
expect(di.sl.isRegistered<InputConverter>(), isTrue);
});
test('PillBoxSetLocalDataSource', () {
expect(di.sl.isRegistered<PillBoxSetLocalDataSource>(), isTrue);
});
test('PillBoxSetRemoteDataSource', () {
expect(di.sl.isRegistered<PillBoxSetRemoteDataSource>(), isTrue);
});
test('PillBoxSetRepository', () {
expect(di.sl.isRegistered<PillBoxSetRepository>(), isTrue);
});
test('GetPillBoxSet', () {
expect(di.sl.isRegistered<GetPillBoxSet>(), isTrue);
});
test('PillBoxSetBloc', () {
expect(di.sl.isRegistered<PillBoxSetBloc>(), isTrue);
});
});
}
The failing dependency:
import 'package:dartz/dartz.dart';
import '../../error/failure.dart';
class InputConverter {
static final InputConverter _instance = InputConverter._();
factory InputConverter() => _instance;
InputConverter._() { }
Either<Failure, int> toUnsignedInteger(String s) {
try {
var i = int.parse(s);
if (i < 0) throw FormatException();
return Right(i);
} on FormatException {
return Left(InvalidInputFailure());
}
}
Either<Failure, String> toWordString(String s) {
if (s == null) {
return Left(InvalidInputFailure());
}
String trimmedString = s.trim();
if (trimmedString.isEmpty) {
return Left(InvalidInputFailure());
}
return Right(trimmedString);
}
}
class InvalidInputFailure extends Failure {}
I have read the docs, searched and looked at Flutter GetIt Plugin - No type xxx is registered inside GetIt to no avail. I realize I am missing something fundamental.