http_client_helper 0.3.2 copy "http_client_helper: ^0.3.2" to clipboard
http_client_helper: ^0.3.2 copied to clipboard

A Flutter plugin for http request with cancel and retry fuctions.

example/lib/main.dart

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:http_client_helper/http_client_helper.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: MyHomePage(title: 'http client helper'),
    );
  }
}

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

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  CancellationToken? cancellationToken;
  String msg = "";

  void request() async {
    var url = "https://api.tuchong.com/feed-app";
    //url = "https://google.com";
    setState(() {
      msg = "begin request";
    });
    cancellationToken = new CancellationToken();
    try {
      await HttpClientHelper.get(
        url,
        cancelToken: cancellationToken,
        timeRetry: Duration(milliseconds: 100),
        retries: 3,
        timeLimit: Duration(seconds: 5),
      ).then((response) {
        setState(() {
          msg = response?.body ?? "";
        });
      });
    } on TimeoutException catch (_) {
      setState(() {
        msg = "TimeoutException";
      });
    } on OperationCanceledError catch (_) {
      setState(() {
        msg = "cancel";
      });
    } catch (e) {
      setState(() {
        msg = "$e";
      });
    }
  }

  void cancel() {
    cancellationToken?.cancel();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(widget.title)),
      body: Column(
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              TextButton(onPressed: request, child: Text("Requset")),
              TextButton(onPressed: cancel, child: Text("Cancel"))
            ],
          ),
          Expanded(child: Text(msg))
        ],
      ),
    );
  }
}
29
likes
140
pub points
92%
popularity

Publisher

verified publisherfluttercandies.com

A Flutter plugin for http request with cancel and retry fuctions.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

http

More

Packages that depend on http_client_helper