Get Data From Api :
main.dart
import 'package:http/http.dart' as http;
import 'package:http/http.dart';
import 'http_retry.dart';
main() async {
var client = new RetryClient(new http.Client());
http.Response response = await client.get('api name');
print('Tag' + response.body.toString());
client.close();
}
http_retry.dart
import 'dart:async';
import 'dart:math' as math;
import 'package:async/async.dart';
import 'package:http/http.dart';
import 'package:pedantic/pedantic.dart';
class RetryClient extends BaseClient {
final Client _inner;
final int _retries;
final bool Function(BaseResponse) _when;
final bool Function(dynamic, StackTrace) _whenError;
final Duration Function(int) _delay;
final void Function(BaseRequest, BaseResponse, int) _onRetry;
RetryClient(this._inner,
{int retries,
bool when(BaseResponse response),
bool whenError(error, StackTrace stackTrace),
Duration delay(int retryCount),
void onRetry(BaseRequest request, BaseResponse response, int retryCount)})
: _retries = retries ?? 3,
_when = when ?? ((response) => response.statusCode == 503),
_whenError = whenError ?? ((_, __) => false),
_delay = delay ??
((retryCount) =>
Duration(milliseconds: 500) * math.pow(1.5, retryCount)),
_onRetry = onRetry {
RangeError.checkNotNegative(_retries, "retries");
}
RetryClient.withDelays(Client inner, Iterable<Duration> delays,
{bool when(BaseResponse response),
bool whenError(error, StackTrace stackTrace),
void onRetry(BaseRequest request, BaseResponse response, int retryCount)})
: this._withDelays(inner, delays.toList(),
when: when, whenError: whenError, onRetry: onRetry);
RetryClient._withDelays(Client inner, List<Duration> delays,
{bool when(BaseResponse response),
bool whenError(error, StackTrace stackTrace),
void onRetry(BaseRequest request, BaseResponse response, int retryCount)})
: this(inner,
retries: delays.length,
delay: (retryCount) => delays[retryCount],
when: when,
whenError: whenError,
onRetry: onRetry);
@override
Future<StreamedResponse> send(BaseRequest request) async {
var splitter = StreamSplitter(request.finalize());
var i = 0;
for (;;) {
StreamedResponse response;
try {
response = await _inner.send(_copyRequest(request, splitter.split()));
} catch (error, stackTrace) {
if (i == _retries || !_whenError(error, stackTrace)) rethrow;
}
if (response != null) {
if (i == _retries || !_when(response)) return response;
unawaited(response.stream.listen((_) {}).cancel()?.catchError((_) {}));
}
await Future.delayed(_delay(i));
if (_onRetry != null) _onRetry(request, response, i);
i++;
}
}
StreamedRequest _copyRequest(BaseRequest original, Stream<List<int>> body) {
var request = StreamedRequest(original.method, original.url);
request.contentLength = original.contentLength;
request.followRedirects = original.followRedirects;
request.headers.addAll(original.headers);
request.maxRedirects = original.maxRedirects;
request.persistentConnection = original.persistentConnection;
body.listen(request.sink.add,
onError: request.sink.addError,
onDone: request.sink.close,
cancelOnError: true);
return request;
}
@override
void close() => _inner.close();
}
pubspec.yaml
name: fluttertutorial
description: A new Flutter application.
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
intl:
http:
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true