搜索插件、代码仓与 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.

所属分类

源码仓库

使用此插件的项目

暂无相关数据。