搜索插件、代码仓与 APP 分类

插件详情v0.1.36

api_manager

一个简单的包,借助 flutter dio api 轻松管理 API 请求。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
301
喜欢数
25
Pub 分数
140

安装插件

flutter pub add api_manager

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个轻量级的 Flutter 包,旨在使用 Dio HTTP 客户端简化 API 请求管理。它提供了一个易于使用的接口,用于发起 HTTP 调用、处理响应以及在项目中一致地管理错误。专为开发者体验而设计,可简化常见的任务,如身份验证头、请求拦截和响应解析。非常适合需要可靠且可维护网络通信的应用程序。

适合场景

  • 处理 REST API 调用
  • 管理身份验证令牌
  • 集中化 API 端点
  • 错误处理和重试逻辑
  • 请求/响应日志
  • 简化网络层代码

主要特点

  • 基于 Dio 构建
  • 轻松配置和设置
  • 支持拦截器
  • 自动错误处理
  • 模块化和可重用的设计
  • 关注点的清晰分离

适合对象

  • Flutter 初学者学习网络编程
  • 需要一致 API 模式的项目
  • 具有多个 API 端点的应用程序
  • 注重可维护代码的团队
  • 希望代码量最少的开发者

使用建议

  • 需要了解 Dio 的基础知识
  • 仅限 HTTP 请求(不支持 WebSocket)
  • 可能需要额外设置以处理复杂的认证流程
  • 未经定制,不适合大规模企业应用

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

api_manager pub package

A simple flutter API to manage rest api request easily with the help of flutter dio.

Get started

Install

Add dependency

pub package

dependencies:
  api_manager: $latest_version
Super simple to use
import 'package:api_manager/api_manager.dart';

void main() async {
 
  ApiResponse response = await ApiManager().request(
    requestType: RequestType.GET,
    route: "your route",
  );
  print(response);
}
Config in a base manager

class ApiRepository {
  
  static final ApiRepository _instance = ApiRepository._internal(); /// singleton api repository
  ApiManager _apiManager;

  factory ApiRepository() {
    return _instance;
  }

  /// base configuration for api manager
  ApiRepository._internal() {
    _apiManager = ApiManager();
    _apiManager.options.baseUrl = BASE_URL; /// EX: BASE_URL = https://google.com/api/v1 
    _apiManager.options.connectTimeout = 100000;
    _apiManager.options.receiveTimeout = 100000;
    _apiManager.responseBodyWrapper('data'); /// This is used to parse the response without data attribute, some use case will shown below
    _apiManager.enableLogging(responseBody: true, requestBody: false); /// enable api logging EX: response, request, headers etc
    _apiManager.enableAuthTokenCheck(() => "access_token"); /// EX: JWT/PASSPORT auth token store in cache
  }
}

Examples

Suppose we have a response model like this:

class SampleResponse{
  String name;
  int id;

  SampleResponse.fromJson(jsonMap): 
        this.name = jsonMap['name'],
        this.id = jsonMap['id'];
}

and actual api response json structure is:

{
    "data": {
        "name": "md afratul kaoser taohid",
        "id": "id"
    }
}

#Now we Performing a GET request :

 Future<ApiResponse<SampleResponse>> getRequestSample() async =>
      await _apiManager.request<SampleResponse>(
        requestType: RequestType.GET,
        route: 'api_route',
        requestParams: {"userId": 12}, /// add params if required
        isAuthRequired: true, /// by set it to true, this request add a header authorization from this method enableAuthTokenCheck();
        responseBodySerializer: (jsonMap) {
          return SampleResponse.fromJson(jsonMap); /// parse the json response into dart model class
        },
      );

#Now we Performing a POST request :

 Future<ApiResponse<SampleResponse>> postRequestSample() async =>
      await _apiManager.request<SampleResponse>(
        requestType: RequestType.POST,
        route: 'api_route',
        requestBody: {"userId": 12}, /// add POST request body
        isAuthRequired: true, /// by set it to true, this request add a header authorization from this method enableAuthTokenCheck();
        responseBodySerializer: (jsonMap) {
          return SampleResponse.fromJson(jsonMap); /// parse the json response into dart model class
        },
      );

#Now er performing a multipart file upload request :

  Future<ApiResponse<void>> updateProfilePicture(
    String filePath,
  ) async {
    MultipartFile multipartFile =
        await _apiManager.getMultipartFileData(filePath);
    FormData formData = FormData.fromMap({'picture': multipartFile});

    return await _apiManager.request(
      requestType: RequestType.POST,
      isAuthRequired: true,
      requestBody: formData,
      route: 'api_route',
    );
  }

所属分类

依赖

依赖清单 2 项

源码仓库

使用此插件的项目

暂无相关数据。