get method
- required String endpoint,
- Object? data,
- Map<
String, dynamic> ? params, - Duration? receiveTimeout,
- Duration? sendTimeout,
- Map<
String, String> ? headers, - ProgressCallback? onSendProgress,
- ProgressCallback? onReceiveProgress,
- CancelToken? cancelToken,
- bool showLoader = true,
- CachePolicy? cachePolicy,
- Duration? cacheMaxAge,
- bool dedupe = true,
Sends a GET request to endpoint.
Use for fetching resources that don't require a request body. Query
parameters go in params.
Returns Right(Response) on success, Left(Failure) on any error
(network, timeout, 4xx/5xx, no internet).
Example:
final result = await _api.get(
endpoint: '/users',
params: {'page': 1, 'limit': 20},
);
result.fold(
(failure) => emit(ErrorState(failure.message)),
(response) => emit(LoadedState(UserListModel.fromJson(response.data))),
);
Caching. Omit cachePolicy and the project-wide
ApiConfig.defaultCachePolicy applies — itself CachePolicy.networkOnly
unless changed, which ignores the cache entirely. Anything else stores
successful responses and can serve them back; see CachePolicy.
cacheMaxAge bounds how old a cached entry may be before it counts as a
miss.
A project can forbid caching outright with
ApiConfig(cacheEnabled: false), which overrides whatever is passed
here.
De-duplication. Two identical GETs in flight at the same time share
one network call by default. Pass dedupe: false to force a separate
request. Automatically skipped when you supply your own cancelToken,
since cancelling one caller must not cancel the other.
Implementation
@override
Future<Either<Failure, Response>> get({
required String endpoint,
Object? data,
Map<String, dynamic>? params,
Duration? receiveTimeout,
Duration? sendTimeout,
Map<String, String>? headers,
ProgressCallback? onSendProgress,
ProgressCallback? onReceiveProgress,
CancelToken? cancelToken,
bool showLoader = true,
CachePolicy? cachePolicy,
Duration? cacheMaxAge,
bool dedupe = true,
}) {
return _respond(HttpMethod.get, endpoint,
data: data, params: params, headers: headers);
}