impaktfull_network_interceptor 0.0.1 copy "impaktfull_network_interceptor: ^0.0.1" to clipboard
impaktfull_network_interceptor: ^0.0.1 copied to clipboard

A lightweight Flutter package to intercept and inspect Dio network calls in-memory, with a built-in review UI.

example/lib/main.dart

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:impaktfull_network_interceptor/impaktfull_network_interceptor.dart';
import 'package:impaktfull_ui/impaktfull_ui.dart';

void main() {
  runApp(const ExampleApp());
}

final _controller = ImpaktfullNetworkInterceptorController();
final _dio = Dio()
  ..interceptors.add(ImpaktfullNetworkDioInterceptor(controller: _controller));

class ExampleApp extends StatelessWidget {
  const ExampleApp({super.key});

  @override
  Widget build(BuildContext context) {
    return ImpaktfullUiApp(
      title: 'Network Interceptor Example',
      home: const ExampleHomeScreen(),
    );
  }
}

class ExampleHomeScreen extends StatefulWidget {
  const ExampleHomeScreen({super.key});

  @override
  State<ExampleHomeScreen> createState() => _ExampleHomeScreenState();
}

class _ExampleHomeScreenState extends State<ExampleHomeScreen> {
  final _listener = _ExampleNetworkInterceptorListener();

  @override
  void initState() {
    super.initState();
    _controller.addListener(_listener);
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _controller.showOverlay(context);
    });
  }

  @override
  void dispose() {
    _controller.removeListener(_listener);
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return ImpaktfullUiScreen(
      title: 'Network Interceptor Demo',
      isFullScreen: true,
      actions: [
        ValueListenableBuilder<int>(
          valueListenable: _controller.callCountNotifier,
          builder: (context, count, _) => ImpaktfullUiIconButton(
            asset: const ImpaktfullUiAsset.icon(Icons.list_alt_outlined),
            tooltip: 'View intercepted calls',
            showNotificationBadge: count > 0,
            notificationBadgeText: count > 0 ? '$count' : null,
            onTap: () => _controller.show(context),
          ),
        ),
      ],
      child: ImpaktfullUiListView(
        padding: const EdgeInsets.all(16),
        spacing: 12,
        children: [
          ImpaktfullUiButton(
            type: ImpaktfullUiButtonType.primary,
            leadingAsset:
                const ImpaktfullUiAsset.icon(Icons.cloud_download_outlined),
            title: 'GET request',
            fullWidth: true,
            onAsyncTap: _onGetTapped,
          ),
          ImpaktfullUiButton(
            type: ImpaktfullUiButtonType.primary,
            leadingAsset:
                const ImpaktfullUiAsset.icon(Icons.cloud_upload_outlined),
            title: 'POST request',
            fullWidth: true,
            onAsyncTap: _onPostTapped,
          ),
          ImpaktfullUiButton(
            type: ImpaktfullUiButtonType.secondary,
            leadingAsset: const ImpaktfullUiAsset.icon(Icons.error_outline),
            title: 'Failing request (404)',
            fullWidth: true,
            onAsyncTap: _onFailingRequestTapped,
          ),
          ImpaktfullUiButton(
            type: ImpaktfullUiButtonType.secondary,
            leadingAsset: const ImpaktfullUiAsset.icon(Icons.wifi_off_outlined),
            title: 'Invalid domain',
            fullWidth: true,
            onAsyncTap: _onInvalidDomainTapped,
          ),
        ],
      ),
    );
  }

  Future<void> _onGetTapped() async {
    await _get('https://jsonplaceholder.org/posts/1');
  }

  Future<void> _onPostTapped() async {
    await _post(
      'https://jsonplaceholder.org/posts/1',
      data: {'title': 'Test', 'body': 'Hello world', 'userId': 1},
    );
  }

  Future<void> _onFailingRequestTapped() async {
    await _get('https://impaktfull.com/something-that-does-not-exist');
  }

  Future<void> _onInvalidDomainTapped() async {
    await _get('https://doesnotexist.com/something-that-does-not-exist');
  }

  Future<void> _get(String url) async {
    try {
      await _dio.get(url);
      ImpaktfullUiNotification.show(
        title: 'Success',
        subtitle: 'The request was successful',
        type: ImpaktfullUiNotificationType.success,
      );
    } catch (_) {
      ImpaktfullUiNotification.show(
        title: 'Error',
        subtitle: 'An error occurred while making the request',
        type: ImpaktfullUiNotificationType.error,
      );
    }
  }

  Future<void> _post(
    String url, {
    required Map<String, dynamic> data,
  }) async {
    try {
      await _dio.post(url, data: data);
      ImpaktfullUiNotification.show(
        title: 'Success',
        subtitle: 'The request was successful',
        type: ImpaktfullUiNotificationType.success,
      );
    } catch (_) {
      ImpaktfullUiNotification.show(
        title: 'Error',
        subtitle: 'An error occurred while making the request',
        type: ImpaktfullUiNotificationType.error,
      );
    }
  }
}

class _ExampleNetworkInterceptorListener
    implements ImpaktfullNetworkInterceptorListener {
  @override
  void onNetworkCallAdded(ImpaktfullNetworkCall call) {
    debugPrint('[NetworkInterceptor] ${call.method} ${call.uri}');
  }
}
1
likes
140
points
13
downloads

Documentation

API reference

Publisher

verified publisherimpaktfull.com

Weekly Downloads

A lightweight Flutter package to intercept and inspect Dio network calls in-memory, with a built-in review UI.

Homepage

License

BSD-3-Clause (license)

Dependencies

dio, flutter, impaktfull_ui

More

Packages that depend on impaktfull_network_interceptor