embrace_dio 2.0.0 copy "embrace_dio: ^2.0.0" to clipboard
embrace_dio: ^2.0.0 copied to clipboard

Allows automatic Dio network capture when using the the embrace plugin.

example/lib/main.dart

// ignore_for_file: inference_failure_on_function_invocation

import 'package:dio/dio.dart';
import 'package:embrace/embrace.dart';
import 'package:embrace_dio/embrace_dio.dart';
import 'package:flutter/material.dart';

Future<void> main() async {
  await Embrace.instance.start(() => runApp(const EmbraceDioDemo()));
}

class EmbraceDioDemo extends StatelessWidget {
  const EmbraceDioDemo({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(home: EmbraceDioMenu());
  }
}

class EmbraceDioMenu extends StatefulWidget {
  const EmbraceDioMenu({Key? key}) : super(key: key);

  @override
  State<EmbraceDioMenu> createState() => _EmbraceDioMenuState();
}

class _EmbraceDioMenuState extends State<EmbraceDioMenu> {
  late final _dio = Dio();

  @override
  void initState() {
    super.initState();
    _dio.interceptors.add(EmbraceInterceptor());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Network')),
      body: Padding(
        padding: const EdgeInsets.all(8),
        child: Center(
          child: Column(
            children: <Widget>[
              ElevatedButton(
                onPressed: () => sendAndLogRequest(HttpMethod.get),
                child: const Text('Get'),
              ),
              ElevatedButton(
                onPressed: () => sendAndLogRequest(HttpMethod.post),
                child: const Text('Post'),
              ),
              ElevatedButton(
                onPressed: () => sendAndLogRequest(HttpMethod.put),
                child: const Text('Put'),
              ),
              ElevatedButton(
                onPressed: () => sendAndLogRequest(HttpMethod.patch),
                child: const Text('Patch'),
              ),
              ElevatedButton(
                onPressed: () => sendAndLogRequest(HttpMethod.delete),
                child: const Text('Delete'),
              ),
              ElevatedButton(
                onPressed: error404Request,
                child: const Text('Error: 404'),
              ),
              ElevatedButton(
                onPressed: invalidDomainRequest,
                child: const Text('Error: Invalid domain'),
              ),
            ],
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    _dio.close();
    super.dispose();
  }

  Future<void> sendAndLogRequest(HttpMethod method) async {
    switch (method) {
      case HttpMethod.put:
        await _dio.put('https://httpbin.org/put');
        break;
      case HttpMethod.post:
        await _dio.post('https://httpbin.org/post');
        break;
      case HttpMethod.patch:
        await _dio.patch('https://httpbin.org/patch');
        break;
      case HttpMethod.delete:
        await _dio.delete('https://httpbin.org/delete');
        break;
      case HttpMethod.get:
        await _dio.get('https://httpbin.org/get');
        break;
      case HttpMethod.other:
        await _dio.get('https://httpbin.org/get');
        break;
    }
  }

  Future<void> error404Request() async {
    await _dio.get('https://httpbin.org/status/404');
  }

  Future<void> invalidDomainRequest() async {
    await _dio.get('https://httpbin.invalid');
  }
}
0
likes
150
points
450
downloads

Publisher

verified publisherembrace.io

Weekly Downloads

Allows automatic Dio network capture when using the the embrace plugin.

Homepage

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

build_runner, build_version, dio, embrace, embrace_platform_interface, flutter, platform, plugin_platform_interface

More

Packages that depend on embrace_dio