Skip to main content

Network Requests

If you are using the http package or the Dio package to perform your network operations, Embrace can capture that information with minimal modification to your codebase. Also, if you are using other method, network requests can be manually logged through the Embrace SDK.

Using the http package

If you are using the http package, you should use an instance of EmbraceHttpClient to perform network operations, like this:

void main() async {
final client = EmbraceHttpClient();
final response = client.get(Uri.parse(''));

This can also be used with existing Clients to add in Embrace's logging functionality. For example:

void main() async {
final baseClient = MyCustomClient();
final client = EmbraceHttpClient(innerClient: baseClient);
final response = client.get(Uri.parse(''));

Be sure to close your client when it's no longer needed by calling client.close().

Using Dio

When using the Dio package, an Embrace interceptor can be added to allow for automatic capture of the requests:

import 'package:dio/dio.dart';
import 'package:embrace_dio/embrace_dio.dart';

var dio = Dio();

Keep in mind that you will need to add the embrace_dio package to your pubspec.yaml to be able to use EmbraceInterceptor.

Manually logging network requests

Alternatively, you can log any network request using this method from the Embrace SDK:

/// Log a network request.
/// Use the [error] parameter to pass the reason phrase or exception message
/// for requests that are not successful.
/// [startTime] and [endTime] should be Unix timestamps, or the
/// number of milliseconds since 1970-01-01T00:00:00Z (UTC),
/// e.g. ``.
void logNetworkRequest({
required String url,
required HttpMethod method,
required int startTime,
required int endTime,
required int bytesSent,
required int bytesReceived,
required int statusCode,
String? error,
String? traceId,