@@ -30,6 +30,7 @@ SimpleQueryCubit<TOut> useQueryCubit<TOut>(
3030 RequestMode ? requestMode,
3131 bool callOnCreate = true ,
3232 List <Object ?> keys = const [],
33+ EmptyChecker <TOut >? isEmpty,
3334}) {
3435 return useBloc (
3536 () {
@@ -47,6 +48,61 @@ SimpleQueryCubit<TOut> useQueryCubit<TOut>(
4748 );
4849}
4950
51+ /// Simplified implementation of [QueryCubit] created in order to be used by [useQueryCubit] .
52+ /// Differ from [SimpleQueryCubit] because it uses a custom function to check if the data is empty.
53+ class SimpleQueryWithEmptyCubit <TOut > extends QueryCubit <TOut , TOut > {
54+ /// Creates a new [SimpleQueryWithEmptyCubit] .
55+ SimpleQueryWithEmptyCubit (
56+ super .loggerTag,
57+ this ._customRequest,
58+ this ._isEmpty, {
59+ super .requestMode,
60+ });
61+
62+ /// The request to be executed.
63+ final Request <QueryResult <TOut >> _customRequest;
64+
65+ /// The function to check if the data is empty.
66+ final EmptyChecker <TOut > _isEmpty;
67+
68+ @override
69+ Future <QueryResult <TOut >> request () => _customRequest ();
70+
71+ @override
72+ TOut map (TOut data) => data;
73+
74+ @override
75+ bool isEmpty (TOut data) => _isEmpty (data);
76+ }
77+
78+ /// Provides a [QueryCubit] specialized for [QueryResult] that is automatically disposed without having
79+ /// to use BlocProvider and does not require any arguments. It is a wrapper of [useBloc] that creates a [SimpleQueryWithEmptyCubit] .
80+ /// Differ from [useQueryCubit] because it uses a custom function to check if the data is empty.
81+ SimpleQueryWithEmptyCubit <TOut > useQueryWithEmptyCubit <TOut >(
82+ Request <QueryResult <TOut >> request,
83+ EmptyChecker <TOut > isEmpty, {
84+ String loggerTag = 'SimpleQueryWithEmptyCubit' ,
85+ RequestMode ? requestMode,
86+ bool callOnCreate = true ,
87+ List <Object ?> keys = const [],
88+ }) {
89+ return useBloc (
90+ () {
91+ final cubit = SimpleQueryWithEmptyCubit <TOut >(
92+ loggerTag,
93+ request,
94+ isEmpty,
95+ requestMode: requestMode,
96+ );
97+ if (callOnCreate) {
98+ cubit.run ();
99+ }
100+ return cubit;
101+ },
102+ keys,
103+ );
104+ }
105+
50106/// Simplified implementation of [ArgsQueryCubit] created in order to be used by [useArgsQueryCubit] .
51107class SimpleArgsQueryCubit <TArgs , TOut >
52108 extends ArgsQueryCubit <TArgs , TOut , TOut > {
@@ -84,3 +140,53 @@ SimpleArgsQueryCubit<TArgs, TOut> useArgsQueryCubit<TArgs, TOut>(
84140 keys,
85141 );
86142}
143+
144+ /// Simplified implementation of [ArgsQueryCubit] created in order to be used by [useArgsQueryCubit] .
145+ /// Differ from [SimpleArgsQueryCubit] because it uses a custom function to check if the data is empty.
146+ class SimpleArgsQueryWithEmptyCubit <TArgs , TOut >
147+ extends ArgsQueryCubit <TArgs , TOut , TOut > {
148+ /// Creates a new [SimpleArgsQueryWithEmptyCubit] .
149+ SimpleArgsQueryWithEmptyCubit (
150+ super .loggerTag,
151+ this ._customRequest,
152+ this ._isEmpty, {
153+ super .requestMode,
154+ });
155+
156+ /// The request to be executed.
157+ final ArgsRequest <TArgs , QueryResult <TOut >> _customRequest;
158+
159+ /// The function to check if the data is empty.
160+ final EmptyChecker <TOut > _isEmpty;
161+
162+ @override
163+ Future <QueryResult <TOut >> request (TArgs args) => _customRequest (args);
164+
165+ @override
166+ TOut map (TOut data) => data;
167+
168+ @override
169+ bool isEmpty (TOut data) => _isEmpty (data);
170+ }
171+
172+ /// Provides a [ArgsQueryCubit] specialized for [QueryResult] that is automatically disposed without having
173+ /// to use BlocProvider and requires arguments. It is a wrapper of [useBloc] that creates a [SimpleArgsQueryWithEmptyCubit] .
174+ /// Differ from [useArgsQueryCubit] because it uses a custom function to check if the data is empty.
175+ SimpleArgsQueryWithEmptyCubit <TArgs , TOut >
176+ useArgsQueryWithEmptyCubit <TArgs , TOut >(
177+ ArgsRequest <TArgs , QueryResult <TOut >> request,
178+ EmptyChecker <TOut > isEmpty, {
179+ String loggerTag = 'SimpleArgsQueryWithEmptyCubit' ,
180+ RequestMode ? requestMode,
181+ List <Object ?> keys = const [],
182+ }) {
183+ return useBloc (
184+ () => SimpleArgsQueryWithEmptyCubit <TArgs , TOut >(
185+ loggerTag,
186+ request,
187+ isEmpty,
188+ requestMode: requestMode,
189+ ),
190+ keys,
191+ );
192+ }
0 commit comments