搜尋套件、儲存庫與 APP 分類

套件詳情v0.0.6

cubit_base

一個輕量級的 Flutter 庫,用於使用 Bloc/Cubit 簡化資料擷取和分頁。它提供標準化的狀態和 Fetcher 工具,以減少重複程式碼。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
49
喜歡數
1
Pub 分數
155

安裝套件

flutter pub add cubit_base

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

cubit_base 是一個輕量級的 Flutter 庫,專為簡化 Bloc/Cubit 模式中的資料擷取與分頁而設計。它提供標準化的狀態管理與內建的 Fetcher 工具,能大幅減少重複程式碼,讓處理載入、成功與錯誤狀態變得更簡單。透過減少樣板程式碼,它加速開發流程,同時在跨平台環境中維持清晰且可預測的狀態轉換。

適用情境

  • 簡化 API 數據獲取
  • 實現分頁列表
  • 減少狀態管理樣板程式碼
  • 構建可重用的數據提供者
  • 管理加載和錯誤狀態
  • 簡化 CRUD 操作

主要特色

  • 用於一致 UI 處理的標準狀態
  • 用於異步操作的內建 Fetcher 工具
  • 開箱即用的分頁支援
  • 最小化設定,結構清晰
  • 跨平台相容性
  • 關注點的清晰分離

適合對象

  • 使用 Bloc/Cubit 模式的開發人員
  • 需要頻繁資料擷取的專案
  • 致力於減少樣板程式碼的團隊
  • 包含分頁內容的應用程式
  • 初學者學習狀態管理
  • 快速原型設計與最小可行產品開發

使用建議

  • 需要熟悉 Bloc/Cubit
  • 複雜狀態邏輯的自訂功能有限
  • 不適合高度動態的狀態流程
  • 最適合簡單的資料操作
  • 可能需要額外的層級來實現進階快取
  • 依賴外部相依性以實現完整功能

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

cubit_base

A lightweight Flutter library to simplify data fetching and pagination using Bloc/Cubit. It provides standardized states and a Fetcher utility to reduce boilerplate in your Clean Architecture features.

Features

  • 🚀 Standardized States: BaseState and BasePaginationState for consistent UI handling.
  • 🔄 Simplified Fetching: Fetcher.fetchWithBase handles loading, success, and error states automatically.
  • 📑 Built-in Pagination: Fetcher.fetchWithPaginate manages page increments and "reached max" logic.
  • 🏗️ Clean Architecture Friendly: Designed to work seamlessly with UseCases and DataStates.

Installation

Add cubit_base to your pubspec.yaml:

dependencies:
  cubit_base: ^0.0.5

Usage

1. Simple Data Fetching

Define your state using BaseState:

class UserState {
  final BaseState<User> user;
  
  UserState({required this.user});
  
  UserState copyWith({BaseState<User>? user}) => UserState(user: user ?? this.user);
}

Use the Fetcher in your Cubit:

Future<void> getUser() async {
  await Fetcher.fetchWithBase<User>(
    fetcher: () => _getUserUseCase.call(),
    state: state.user,
    emitter: (newState) => emit(state.copyWith(user: newState)),
  );
}
2. Pagination

Define your state using BasePaginationState:

class ProductState {
  final BasePaginationState<Product> products;

  ProductState({required this.products});

  ProductState copyWith({BasePaginationState<Product>? products}) =>
      ProductState(products: products ?? this.products);
}

Implement pagination in your Cubit:

Future<void> getProducts({bool isRefresh = false}) async {
  await Fetcher.fetchWithPaginate<Product>(
    fetcher: () => _getProductsUseCase.call(
      params: ProductParams(
        page: isRefresh ? 1 : state.products.query.page,
        size: state.products.query.size,
      ),
    ),
    state: state.products,
    isRefresh: isRefresh,
    emitter: (newState) => emit(state.copyWith(products: newState)),
  );
}

State Components

BaseState<T>

Manages a single object of type T.

  • status: initial, loading, success, error.
  • data: The fetched object.
  • error: Error message if any.
BasePaginationState<T>

Manages a list of type T.

  • list: List of items.
  • status: initial, loading, paging, success, error.
  • query: Contains page and size.
  • reachedMax: Boolean indicating if all data is loaded.

License

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

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。