Chuck
ChuckInterceptor is an HTTP Inspector tool for Flutter which helps debugging http requests. It catches and stores http requests and responses, which can be viewed via simple UI. It is inspired from Chuck and Chucker.
Supported Dart http client plugins:
- Dio
- HttpClient from dart:io package
- Http from http/http package
Features:
✔️ Detailed logs for each HTTP calls (HTTP Request, HTTP Response)
✔️ Inspector UI for viewing HTTP calls
✔️ Save HTTP calls to file
✔️ Statistics
✔️ Notification on HTTP call
✔️ Support for top used HTTP clients in Dart
✔️ Error handling
✔️ Shake to open inspector
✔️ HTTP calls search
Install
- Add this to your pubspec.yaml file:
dependencies:
chuck_interceptor: ^2.1.4
- Install it
$ flutter packages get
- Import it
import 'package:chuck_interceptor/chuck.dart';
Usage
Chuck configuration
- Create chuck instance:
Chuck chuck = Chuck();
- Add navigator key to your application:
MaterialApp( navigatorKey: chuck.getNavigatorKey(), home: ...)
You need to add this navigator key in order to show inspector UI. You can use also your navigator key in Chuck:
Chuck chuck = Chuck(showNotification: true, navigatorKey: yourNavigatorKeyHere);
If you need to pass navigatorKey lazily, you can use:
chuck.setNavigatorKey(yourNavigatorKeyHere);
This is minimal configuration required to run Chuck. Can set optional settings in Chuck constructor, which are presented below. If you don't want to change anything, you can move to Http clients configuration.
Additional settings
You can set showNotification
in Chuck constructor to show notification. Clicking on this notification will open inspector.
Chuck chuck = Chuck(..., showNotification: true);
You can set showInspectorOnShake
in Chuck constructor to open inspector by shaking your device (default disabled):
Chuck chuck = Chuck(..., showInspectorOnShake: true);
If you want to use dark mode just add darkTheme
flag:
Chuck chuck = Chuck(..., darkTheme: true);
If you want to pass another notification icon, you can use notificationIcon
parameter. Default value is @mipmap/ic_launcher.
Chuck chuck = Chuck(..., notificationIcon: "myNotificationIconResourceName");
If you want to limit max numbers of HTTP calls saved in memory, you may use maxCallsCount
parameter.
Chuck chuck = Chuck(..., maxCallsCount: 1000));
If you want to change the Directionality of Chuck, you can use the directionality
parameter. If the parameter is set to null, the Directionality of the app will be used.
Chuck chuck = Chuck(..., directionality: TextDirection.ltr);
HTTP Client configuration
If you're using Dio, you just need to add interceptor.
Dio dio = Dio();
dio.interceptors.add(chuck.getDioInterceptor());
If you're using HttpClient from dart:io package:
httpClient
.getUrl(Uri.parse("https://jsonplaceholder.typicode.com/posts"))
.then((request) async {
Chuck.onHttpClientRequest(request);
var httpResponse = await request.close();
var responseBody = await httpResponse.transform(utf8.decoder).join();
chuck.onHttpClientResponse(httpResponse, request, body: responseBody);
});
If you're using http from http/http package:
http.get('https://jsonplaceholder.typicode.com/posts').then((response) {
chuck.onHttpResponse(response);
});
If you're using Chopper. you need to add interceptor:
chopper = ChopperClient(
interceptors: chuck.getChopperInterceptor(),
);
If you have other HTTP client you can use generic http call interface:
ChuckHttpCall chuckHttpCall = ChuckHttpCall(id);
chuck.addHttpCall(ChuckHttpCall);
Show inspector manually
You may need that if you won't use shake or notification:
chuck.showInspector();
Saving calls
Chuck supports saving logs to your mobile device storage. In order to make save feature works, you need to add in your Android application manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Extensions
You can use extensions to shorten your http and http client code. This is optional, but may improve your codebase. Example:
- Import:
import 'package:chuck_interceptor/core/chuck_http_client_extensions.dart';
import 'package:chuck_interceptor/core/chuck_http_extensions.dart';
- Use extensions:
http
.post('https://jsonplaceholder.typicode.com/posts', body: body)
.interceptWithChuck(Chuck, body: body);
httpClient
.postUrl(Uri.parse("https://jsonplaceholder.typicode.com/posts"))
.interceptWithChuck(chuck, body: body, headers: Map());
Example
See complete example here: https://github.com/SunnatilloShavkatov/chuck_interceptor/blob/master/example/lib/main.dart To run project, you need to call this command in your terminal:
flutter pub run build_runner build --delete-conflicting-outputs
Libraries
- chuck
- core/chuck_core
- core/chuck_dio_interceptor
- core/chuck_http_adapter
- core/chuck_http_client_adapter
- core/chuck_http_client_extensions
- core/chuck_http_extensions
- core/chuck_utils
- helper/chuck_alert_helper
- helper/chuck_conversion_helper
- helper/chuck_save_helper
- model/chuck_form_data_file
- model/chuck_from_data_field
- model/chuck_http_call
- model/chuck_http_error
- model/chuck_http_request
- model/chuck_http_response
- model/chuck_sort_option
- ui/page/chuck_call_details_screen
- ui/page/chuck_calls_list_screen
- ui/page/chuck_stats_screen
- ui/widget/chuck_base_call_details_widget
- ui/widget/chuck_call_error_widget
- ui/widget/chuck_call_list_item_widget
- ui/widget/chuck_call_overview_widget
- ui/widget/chuck_call_request_widget
- ui/widget/chuck_call_response_preview_widget
- ui/widget/chuck_call_response_widget
- ui/widget/chuck_json_viewer
- utils/chuck_constants
- utils/chuck_parser
- utils/shake_detector
- Code from https://github.com/deven98/shake Seems to be not maintained for almost 2 years... (01.03.2021).