Replies: 2 comments 4 replies
-
From how I see it, from the example above as to why, as a project scales registering the repositories in the widget becomes quite disorganised. Example: https://github.com/VGVentures/spacex_demo/blob/main/lib/app/app.dart What happens when you have 10+ repos, depending on the size of the project? It gets more mangled up when you need to inject a repository into a cubit/bloc when it needs to be used in multiple places. See (https://github.com/VGVentures/spacex_demo/blob/main/lib/rockets/view/rockets_page.dart) for an example. Injectable, in my view looks like unnecessary complexity (I am open to having my mind changed). However, final getIt = GetIt.instance;
class Singletons {
static void setup() {
getIt
..registerSingleton<HiveService>(HiveServiceImpl())
..registerSingleton<LocalDBService>(LocalDBServiceImpl())
..registerSingleton<AuthService>(AuthServiceImpl())
..registerSingleton<MissionService>(MissionServiceImpl())
..registerSingleton<NotificationService>(NotificationServiceImpl())
..registerSingleton<SoulService>(SoulServiceImpl())
..registerSingleton<DebriefService>(DebriefServiceImpl())
..registerSingleton<LMSService>(LMSServiceImpl())
..registerSingleton<SocketService>(
SocketServiceImpl(localDBService: getIt()),
)
..registerSingleton<StudentService>(StudentServiceImpl());
}
static List<BlocProvider> registerCubits() {
return <BlocProvider>[
BlocProvider<SigninCubit>(
create: (context) => SigninCubit(
authService: getIt(),
hiveService: getIt(),
socketService: getIt(),
),
),
BlocProvider<RegisterStudentCubit>(
create: (context) => RegisterStudentCubit(
authService: getIt(),
),
),
BlocProvider<SignOutCubit>(
create: (context) => SignOutCubit(
hiveService: getIt(),
localDBService: getIt(),
),
),
// ....
];
}
} Then initialize as follows: Singletons.setup();
await bootstrap(
() => MultiBlocProvider(
providers: Singletons.registerCubits(),
child: const App(),
),
); To use in a widget. // Call the cubit/bloc
PrimaryButton(
onPressed: () {
context.read<SigninCubit>().signIn(
email: _emailController.text.trim(),
password: _passwordController.text.trim(),
);
},
title: 'Sign In',
)
// Call an API to load results
class _EnquiriesPageHandsetState extends State<EnquiriesPageHandset> {
@override
void initState() {
context.read<GetStudentEnquiriesCubit>().getStudentEnquiries();
super.initState();
}
// ...
} In this format, everything is set up in one place and you don't need to run injections in widgets and the definitions are straightforward. |
Beta Was this translation helpful? Give feedback.
-
While I understand and respect your perspective, I'd like to explain why I prefer using injectable in this project, as well as in other projects I maintain or contribute to. For me, injectable serves as a convenient code generator for get_it, which I find to be an essential tool in project development. Here are my main reasons:
Now that I have given you the reasons I will proceed on how to use it.
As highlighted, using I introduced |
Beta Was this translation helpful? Give feedback.
-
I'm curious about the need for get_it/injectable. I understand they are service locators and quite popular within the community, and used it a bit, but I question it's need. Seems we are going for bloc, what advantage does using getit/injectable have over using repository providers and bloc providers? An example of this is here
Beta Was this translation helpful? Give feedback.
All reactions