native_dio_client 0.0.3 copy "native_dio_client: ^0.0.3" to clipboard
native_dio_client: ^0.0.3 copied to clipboard

discontinuedreplaced by: native_dio_adapter

A client for Dio which makes use of cupertino_http and cronet_http to delegate HTTP requests to the native platform.

example/lib/main.dart

import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:native_dio_client/native_dio_client.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        mainAxisSize: MainAxisSize.max,
        children: [
          ElevatedButton(
            onPressed: _doGetRequest,
            child: const Text('make get request'),
          ),
          ElevatedButton(
            onPressed: _doPostRequest,
            child: const Text('make post request'),
          ),
          ElevatedButton(
            onPressed: _doHttpClientRequest,
            child: const Text('make client request'),
          ),
          ElevatedButton(
            onPressed: _doHttpClientPostRequest,
            child: const Text('make client post request'),
          ),
        ],
      ),
    );
  }

  void _doGetRequest() async {
    final dio = Dio();

    dio.httpClientAdapter = NativeAdapter(
      cupertinoConfiguration:
          URLSessionConfiguration.ephemeralSessionConfiguration()
            ..allowsCellularAccess = false
            ..allowsConstrainedNetworkAccess = false
            ..allowsExpensiveNetworkAccess = false,
    );
    final response = await dio.get<String>('https://flutter.dev');

    await showDialog<void>(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text('Response ${response.statusCode}'),
          content: SingleChildScrollView(
            child: Text(response.data ?? 'No response'),
          ),
          actions: [
            MaterialButton(
              onPressed: () => Navigator.pop(context),
              child: const Text('Close'),
            )
          ],
        );
      },
    );
  }

  void _doPostRequest() async {
    final dio = Dio();

    dio.httpClientAdapter = NativeAdapter(
      cupertinoConfiguration:
          URLSessionConfiguration.ephemeralSessionConfiguration()
            ..allowsCellularAccess = false
            ..allowsConstrainedNetworkAccess = false
            ..allowsExpensiveNetworkAccess = false,
    );
    final response = await dio.post<String>(
      'https://httpbin.org/post',
      queryParameters: <String, dynamic>{'foo': 'bar'},
      data: jsonEncode(<String, dynamic>{'foo': 'bar'}),
      options: Options(headers: <String, dynamic>{'foo': 'bar'}),
    );

    await showDialog<void>(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text('Response ${response.statusCode}'),
          content: SingleChildScrollView(
            child: Text(response.data ?? 'No response'),
          ),
          actions: [
            MaterialButton(
              onPressed: () => Navigator.pop(context),
              child: const Text('Close'),
            )
          ],
        );
      },
    );
  }

  void _doHttpClientRequest() async {
    final config = URLSessionConfiguration.ephemeralSessionConfiguration()
      ..allowsCellularAccess = false
      ..allowsConstrainedNetworkAccess = false
      ..allowsExpensiveNetworkAccess = false;
    final client = CupertinoClient.fromSessionConfiguration(config);
    final response = await client.get(Uri.parse('https://flutter.dev/'));
    await showDialog<void>(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text('Response ${response.statusCode}'),
          content: SingleChildScrollView(
            child: Text(response.body),
          ),
          actions: [
            MaterialButton(
              onPressed: () => Navigator.pop(context),
              child: const Text('Close'),
            )
          ],
        );
      },
    );
  }

  void _doHttpClientPostRequest() async {
    final config = URLSessionConfiguration.ephemeralSessionConfiguration()
      ..allowsCellularAccess = false
      ..allowsConstrainedNetworkAccess = false
      ..allowsExpensiveNetworkAccess = false;
    final client = CupertinoClient.fromSessionConfiguration(config);

    final response = await client.post(
      Uri.parse('https://httpbin.org/post'),
      body: jsonEncode(<String, dynamic>{'foo': 'bar'}),
    );

    await showDialog<void>(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text('Response ${response.statusCode}'),
          content: SingleChildScrollView(
            child: Text(response.body),
          ),
          actions: [
            MaterialButton(
              onPressed: () => Navigator.pop(context),
              child: const Text('Close'),
            )
          ],
        );
      },
    );
  }
}
1
likes
130
pub points
33%
popularity

Publisher

verified publisheruekoetter.dev

A client for Dio which makes use of cupertino_http and cronet_http to delegate HTTP requests to the native platform.

Repository (GitHub)
View/report issues

Documentation

API reference

Funding

Consider supporting this project:

github.com
www.buymeacoffee.com

License

Apache-2.0 (LICENSE)

Dependencies

cronet_http, cupertino_http, dio, http

More

Packages that depend on native_dio_client