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

插件详情v0.2.1

jh_services

一个轻量级的 Flutter 服务包,用于无缝处理网络请求、本地存储和连接性,遵循最佳实践。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
714
喜欢数
2
Pub 分数
160

安装插件

flutter pub add jh_services

插件介绍

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

jh_services 是一个轻量级的 Flutter 包,旨在通过最佳实践简化网络请求、本地存储管理和连接性检查。它提供了一个统一且易于使用的接口,用于处理 HTTP 操作、本地缓存数据以及跨平台的设备连接性监控。该包以简洁和可靠性为核心设计,减少了样板代码,提升了开发效率。支持所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux,非常适合需要强大后端集成和离线功能的跨平台应用。

适合场景

  • 处理 REST API 调用
  • 管理本地数据持久化
  • 监控互联网连接
  • 实现离线优先功能
  • 简化服务层逻辑
  • 跨平台数据同步

主要特点

  • 统一服务接口
  • 内置连接性监控
  • 安全的本地存储
  • 自动重试机制
  • 平台无关设计
  • 最小化样板

适合对象

  • 寻求简单服务抽象的Flutter开发人员
  • 需要离线支持的项目
  • 频繁进行网络交互的应用程序
  • 跨平台移动和桌面应用
  • 优先考虑可维护代码结构的团队

使用建议

  • 需要理解 async/await 模式
  • 针对高级网络需求的定制化功能有限
  • 取决于底层 HTTP 客户端实现
  • 不适合高度复杂的API工作流
  • 最适合中等规模的应用
  • 可能需要额外的错误处理以用于生产环境

原始 README

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

jh_services 0.1.1

jh_services is a Flutter package that provides a collection of services for handling common tasks in your Flutter applications, such as shared preferences, network requests, image and file picking, and checking internet connectivity.

Features

  • Shared Preferences Service: Manage local storage using shared preferences.
  • Network Service: Handle network requests with customizable configurations.
  • Image Picker Service: Pick images from the device's gallery or camera.
  • File Picker Service: Select files from the device's file system.
  • Connectivity Service: Check internet connectivity status and listen for changes.

Installation

Add jh_services to your pubspec.yaml file:

dependencies:
  jh_services: ^0.1.1

Usage

1. Setup Service Locator

Before using any of the services, you need to set up the service locator in your Flutter application. You can do this in your main.dart file:

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

void main() {
   setupServiceLocator(
    sharedPrefsConfig:SharedPrefsConfig() ,
    connectivityConfig: ConnectivityConfig(),
    networkConfig: NetworkConfig(baseUrl: 'https://api.example.com',
     defaultHeaders:  {
      'Content-Type': 'application/json',
      // Other headers if necessary
    },),
  );
  
  runApp(MyApp());
}
2. Initialize Services

You should initialize the services before using them. This is typically done in your main.dart file after setting up the service locator:

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  setupServiceLocator(
    sharedPrefsConfig:SharedPrefsConfig() ,
    connectivityConfig: ConnectivityConfig(),
    networkConfig: NetworkConfig(baseUrl: 'https://api.example.com',
     defaultHeaders:  {
      'Content-Type': 'application/json',
      // Other headers if necessary
    },),
  );
  runApp(MyApp());
}
3. Using the Services
Shared Preferences Service
import 'package:jh_services/jh_services.dart';

final sharedPrefsService = serviceLocator<SharedPrefsService>();

// Example of saving a value
await sharedPrefsService.saveString('key', 'value');

// Example of retrieving a value
String? value = sharedPrefsService.getString('key');
  • New Methods:
    • saveString(String key, String value): Save a string value under a specific key.
    • getString(String key): Retrieve a string value by key.
    • saveDateTime(String key, DateTime value): Save a DateTime value as a string.
    • getDateTime(String key): Retrieve a DateTime value.
    • updateOrAppendToStringList(String key, List<String> newList): Update a string list by replacing it if it exists or appending if it doesn't.
    • saveBulk(Map<String, dynamic> data): Save multiple key-value pairs in one go.
    • getBulk(List<String> keys): Retrieve multiple values for a list of keys.
    • removeBulk(List<String> keys): Remove multiple keys.
    • incrementOrInitInt(String key, {int incrementBy = 1, int initialValue = 0}): Increment or initialize an integer value.
    • decrementInt(String key, {int decrementBy = 1}): Decrement an integer value.
    • toggleBool(String key): Toggle a boolean value.
    • addToStringList(String key, String value): Add a single item to a list of strings.
    • removeFromStringList(String key, String value): Remove a single item from a list of strings.
    • getAll(): Retrieve all key-value pairs.
    • saveJson(String key, Map<String, dynamic> json): Save a JSON object by encoding it as a string.
    • getJson(String key): Retrieve a JSON object by decoding a stored string.
    • resetToDefaults(Map<String, dynamic> defaultValues): Reset a list of keys to specified default values.
    • getKeysByPrefix(String prefix): Retrieve all keys that start with a specified prefix.
    • getItemCount(): Count the number of stored items.
    • batchIncrementInts(Map<String, int> increments): Batch update to increment multiple integer keys by specified values.
    • retrieveAndRemove(List<String> keys): Retrieve and remove values for a list of keys.
    • getValue<T>(String key): Retrieve a value by type.
    • getKeys(): Retrieve all keys.
    • clear(): Clear all values.
    • containsKey(String key): Check if a key exists.
    • remove(String key): Remove a value associated with a specific key.
Network Service
import 'package:jh_services/jh_services.dart';

final networkService = serviceLocator<NetworkService>();

// Example of making a GET request
final response = await networkService.get('/endpoint');

// Example of making a POST request
final postResponse = await networkService.post('/endpoint', data: {'key': 'value'});

// Making a POST request with FormData
final formDataResponse = await networkService.post(
  '/upload',
  data: {
    'file': await convertImageToMultipartFile(image), // Example of using FormData
  },
  isFormData: true,
);

Exception Handling in jh_services

The jh_services package provides a robust error-handling mechanism when making network requests using Dio. Each type of network error throws a ServerException, which contains details such as an error message, the specific error type, and a status code.

Error Handling

The following table outlines the status codes returned for each DioExceptionType in case of an error:

Dio Exception Type Status Code Description
DioExceptionType.connectionTimeout 408 The request timed out while connecting to the server.
DioExceptionType.sendTimeout 504 The request timed out while sending data to the server.
DioExceptionType.receiveTimeout 504 The request timed out while receiving data from the server.
DioExceptionType.badCertificate 495 The SSL certificate is invalid.
DioExceptionType.cancel 499 The client canceled the request.
DioExceptionType.connectionError 503 There was a network connection error.
DioExceptionType.unknown 520 An unknown error occurred.
DioExceptionType.badResponse Varies Depends on the status code from the server (e.g., 400, 401, 403, 404, 409, 422, 504).
Example of Handling Exceptions

When you make a network request and an error occurs, a ServerException is thrown, which you can catch and process as follows:

try {
  final response = await networkService.get('/endpoint');
  print(response);
} on ServerException catch (e) {
  // Handle the exception
  print('Error: ${e.errorModel.message}');
  print('Status Code: ${e.errorModel.statusCode}');
}
Image Picker Service
import 'package:jh_services/jh_services.dart';

final imagePickerService = serviceLocator<ImagePickerService>();

// Example of picking an image
final imagePath = await imagePickerService.pickImage();
File Picker Service
import 'package:jh_services/jh_services.dart';

final filePickerService = serviceLocator<FilePickerService>();

// Example of picking a single file
final filePath = await filePickerService.pickSingleFile(allowedExtensions: ['pdf', 'docx']);

// Example of picking multiple files
final filePaths = await filePickerService.pickMultipleFiles();
Connectivity Service
import 'package:jh_services/jh_services.dart';

final connectivityService = serviceLocator<ConnectivityService>();

// Example of checking current connectivity status
final isConnected = await connectivityService.isConnected();


License

This project is licensed under the MIT License. See the LICENSE file for details.

所属分类

依赖

依赖清单 6 项

源码仓库

使用此插件的项目

暂无相关数据。