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

插件详情v8.1.0

json_api

一个与框架无关的 JSON:API 客户端和服务器实现。支持 JSON:API v1.0 (https://jsonapi.org)

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1420
喜欢数
37
Pub 分数
150

安装插件

flutter pub add json_api

插件介绍

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

一个与框架无关的 Dart 实现,用于构建符合 JSON:API v1.0 规范的 JSON:API 客户端和服务器。它通过 JSON:API 规范实现了应用程序与 API 之间的结构化、标准化通信。该包支持所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux,非常适合跨平台项目。它提供了高效解析、序列化和管理 JSON:API 数据负载的工具,且不依赖于任何特定的 UI 框架。

适合场景

  • 构建符合 JSON:API v1.0 的 RESTful API
  • 使用 JSON:API 与后端服务集成
  • 在多个平台上创建统一的数据层
  • 处理 API 响应中的复杂关系
  • 基于 JSON:API 标准实现客户端缓存

主要特点

  • 完整的 JSON:API v1.0 兼容性
  • 框架无关的设计
  • 跨平台支持
  • 强大的请求/响应处理
  • 对包含的资源和关系的支持
  • 轻松序列化/反序列化

适合对象

  • 开发消费或提供 JSON:API 端点的应用程序的开发者
  • 需要在跨平台环境中保持一致的 API 结构的团队
  • 需要标准化数据交换格式的项目
  • 后端开发人员在 Dart 中实现 JSON:API 服务器
  • 使用 Flutter 且具有严格 API 合同要求的前端团队

使用建议

  • 需要了解 JSON:API 规范
  • 不是完整的框架——补充现有的HTTP客户端
  • 有限的内置错误处理,仅限于规范定义的行为
  • 最好与额外的验证或状态管理库一起使用

原始 README

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

JSON:API Client and Server

TL;DR:

import 'package:http/http.dart' as http;
import 'package:http_interop_http/http_interop_http.dart';
import 'package:json_api/client.dart';
import 'package:json_api/routing.dart';

void main() async {
  /// Define the server's base URL
  final baseUri = 'http://localhost:8080';

  /// Use the standard recommended URL structure or implement your own
  final uriDesign = StandardUriDesign(Uri.parse(baseUri));

  /// This is the Dart's standard HTTP client.
  /// Do not forget to close it in the end.
  final httpClient = http.Client();

  /// This is the interface which decouples this JSON:API implementation
  /// from the HTTP client.
  /// Learn more: https://pub.dev/packages/http_interop
  final httpHandler = httpClient.handleInterop;

  /// This is the basic JSON:API client. It is flexible but not very convenient
  /// to use, because you would need to remember a lot of JSON:API protocol details.
  /// We will use another wrapper on top of it.
  final jsonApiClient = Client(httpHandler);

  /// The [RoutingClient] is most likely the right choice.
  /// It is called routing because it routes the calls to the correct
  /// URLs depending on the use case. Take a look at its methods, they cover
  /// all the standard scenarios specified by the JSON:API standard.
  final client = RoutingClient(uriDesign, jsonApiClient);

  try {
    /// Fetch the collection.
    /// See other methods to query and manipulate resources.
    final response = await client.fetchCollection('colors');

    /// The fetched collection allows us to iterate over the resources
    /// and to look into their attributes
    for (final resource in response.collection) {
      final {
      'name': name,
      'red': red,
      'green': green,
      'blue': blue,
      } = resource.attributes;
      print('${resource.type}:${resource.id}');
      print('$name - $red:$green:$blue');
    }
  } on RequestFailure catch (e) {
    /// Catch error response
    for (final error in e.errors) {
      print(error.title);
    }
  }

  /// Free up the resources before exit.
  httpClient.close();
}

This is a work-in-progress. You can help it by submitting a PR with a feature or documentation improvements.

所属分类

依赖

依赖清单 2 项

源码仓库

使用此插件的项目

暂无相关数据。