搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。