igot_http_service_helper 0.7.0 copy "igot_http_service_helper: ^0.7.0" to clipboard
igot_http_service_helper: ^0.7.0 copied to clipboard

This package contains a set of high-level functions and classes that make it easy to consume HTTP resources. It's multi-platform (mobile, desktop, and browser) and supports multiple implementations wi [...]

example/example.dart

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:igot_http_service_helper/igot_http_service.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('HTTP Service Example')),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: () async {
                await ApiService.getApiRequest();
              },
              child: const Text('GET Data (with cache)'),
            ),
            const SizedBox(height: 16),
            ElevatedButton(
              onPressed: () async {
                await ApiService.postApiRequest();
              },
              child: const Text('POST Data'),
            ),
            const SizedBox(height: 16),
          ],
        ),
      ),
    );
  }
}

class ApiService {
  static Future<dynamic> getApiRequest() async {
    // Type param is optional — omit for dynamic, or be explicit:
    // HttpResponse<List<dynamic>> response = await HttpService.get<List<dynamic>>(...)
    final HttpResponse<dynamic> response = await HttpService.get(
      apiUri: Uri.parse('https://jsonplaceholder.typicode.com/posts'),
      ttl: const Duration(minutes: 30),
    );

    var contents =
        response.data is String ? jsonDecode(response.data) : response.data;
    debugPrint('GET Response: ${contents.length} items');
    return contents;
  }

  static Future<dynamic> postApiRequest() async {
    final HttpResponse<dynamic> response = await HttpService.post(
      apiUri: Uri.parse('https://jsonplaceholder.typicode.com/posts'),
      body: {
        'title': 'foo',
        'body': 'bar',
        'userId': 1,
      },
      // POST requests can also be cached
      ttl: const Duration(minutes: 5),
    );

    var contents =
        response.data is String ? jsonDecode(response.data) : response.data;
    debugPrint('POST Response: $contents');
    return contents;
  }
}
0
likes
140
points
267
downloads

Publisher

unverified uploader

Weekly Downloads

This package contains a set of high-level functions and classes that make it easy to consume HTTP resources. It's multi-platform (mobile, desktop, and browser) and supports multiple implementations with DATA Base support with respect to given TTL.

Homepage

Documentation

API reference

License

unknown (license)

Dependencies

crypto, dio, flutter, hive, hive_flutter, path

More

Packages that depend on igot_http_service_helper