-
Notifications
You must be signed in to change notification settings - Fork 3
RocketClient
Mohammed CHAHBOUN edited this page Jan 20, 2026
·
1 revision
RocketClient is a powerful HTTP client built into Flutter Rocket. It handles requests, responses, state transitions in models, and includes features like interceptors and caching.
Initialize the client with a base URL:
final client = RocketClient(url: 'https://api.example.com');Use the request method to fetch data. It automatically handles the RocketModel state.
await client.request(
'posts',
model: postModel,
method: RocketMethods.get, // Default is GET
);-
model: TheRocketModelthat will store the response. -
params: Query parameters. -
data: Request body (for POST/PUT). -
target: If the data you want is nested in the JSON response (e.g.,['data', 'items']).
Interceptors allow you to run code before a request is sent or after a response is received.
final client = Rocketクライアント(
url: 'https://api.example.com',
beforeRequest: (request) {
// Add Auth token
request.headers['Authorization'] = 'Bearer your_token';
return request;
},
afterResponse: (response) {
// Log status or refresh token
print('Status: ${response.statusCode}');
return response;
},
);Speed up your app by caching network responses.
- Initialize Caching:
void main() async {
await RocketCache.init();
runApp(MyApp());
}- Use in Request:
client.request(
'posts',
model: postModel,
cacheKey: 'posts_cache',
cacheDuration: Duration(hours: 1),
);client.sendFile(
'upload',
fields: {'name': 'profile'},
files: {'avatar': 'path/to/image.png'},
);