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

It extends HTTP package then combine with Hive to provide cache. It also supports authorization via custom headers.

example/README.md

Extended HTTP - fetch data from your API with caching and authorization options #

All methods from BaseClient is inherited, including get, post, put, patch and more. See at BaseClient APIs


// Call config at the App init to apply for all following requests, skip to use default config.

ExtendedHttp().config(
  String? baseURL,
  Duration? timeout,
  bool? disableCache,
  Duration? cacheAge,
  Map<String, String>? headers,
);

ExtendedHttp().onUnauthorized = () async {
  // Fetch token
  ExtendedHttp().config(
    headers: // new headers,
  );
};

// Usage: Example Post APIs
class Post {
  static Future<List<Post>> getAll({
    int page = 1,
    int limit = 10,
    String search = '',
  }) async {
    final uri = ExtendedHttp().createURI('/posts', params: {
      "_page": "$page",
      "_limit": "$limit",
      "q": search,
    });
    final res = await ExtendedHttp().get(uri);
    final dataList = jsonDecode(res.body) as List<dynamic>;
    return dataList.map((e) => Post.fromJson(e)).toList();
  }

  static Future<Post> getDetail(int id) async {
    final uri = ExtendedHttp().createURI('/posts/$id');
    final res = await ExtendedHttp().get(uri);
    final data = jsonDecode(res.body) as Map<String, dynamic>;
    return Post.fromJson(data);
  }

  static Future<Post> create(Post newPost) async {
    final uri = ExtendedHttp().createURI('/posts');
    final res = await ExtendedHttp().post(
      uri,
      body: newPost.toJson(),
    );
    final data = jsonDecode(res.body) as Map<String, dynamic>;
    return Post.fromJson(data);
  }

  static Future<Post> update(Post post) async {
    final uri = ExtendedHttp().createURI('/posts/${post.id}');
    final res = await ExtendedHttp().put(
      uri,
      body: post.toJson()..remove('id'),
    );
    final data = jsonDecode(res.body) as Map<String, dynamic>;
    return Post.fromJson(data);
  }

  static Future<Post> delete(int id) async {
    final uri = ExtendedHttp().createURI('/posts/$id');
    final res = await ExtendedHttp().delete(uri);
    final data = jsonDecode(res.body) as Map<String, dynamic>;
    return Post.fromJson(data);
  }
}
3
likes
0
pub points
52%
popularity

Publisher

verified publisherbesoft.vn

It extends HTTP package then combine with Hive to provide cache. It also supports authorization via custom headers.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter, hive, hive_flutter, http

More

Packages that depend on extended_http