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

插件详情v3.5.0

hooks_riverpod

一个响应式缓存和数据绑定框架。Riverpod 让处理异步代码变得轻而易举。

AndroidiOSmacOSWindowsLinux
近 30 天下载
70.1万
喜欢数
932
Pub 分数
150

安装插件

flutter pub add hooks_riverpod

插件介绍

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

Hooks Riverpod 是一个用于 Flutter 的响应式状态管理和依赖注入框架,可简化异步操作和数据绑定的处理。基于 Riverpod 生态系统构建,它与 Flutter 的 hooks 系统无缝集成,使开发者能够使用函数式组件高效管理状态。它提供自动响应、缓存以及清晰的关注点分离,使复杂的状态逻辑更易于维护和测试。

适合场景

  • 管理应用范围的状态
  • 处理异步数据加载
  • 响应式 UI 更新
  • 大型应用中的依赖注入
  • 测试有状态逻辑

主要特点

  • 响应式状态更新
  • 内置缓存
  • 对异步操作的支持
  • 与 Flutter hooks 的集成
  • 细粒度依赖控制

适合对象

  • 大规模 Flutter 应用
  • 注重可测试性的团队
  • 使用函数式 widget 模式的开发者
  • 具有复杂状态交互的应用程序
  • 需要依赖注入的项目

使用建议

  • 初学者的学习曲线更陡峭
  • 需要理解 Dart 的 async/await
  • 可能在简单应用中引入开销
  • 与其他替代方案相比,文档有限
  • 需要仔细的结构设计以避免性能问题

原始 README

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

Build Status codecov Star on Github License: MIT Discord

Deploys by Netlify

Riverpod


A reactive caching and data-binding framework. https://riverpod.dev Riverpod makes working with asynchronous code a breeze by:

  • Handling errors/loading states by default. No need to manually catch errors
  • Natively supporting advanced scenarios, such as pull-to-refresh
  • Separating the logic from your UI
  • Ensuring your code is testable, scalable and reusable
riverpod pub package
flutter_riverpod pub package
hooks_riverpod pub package

Welcome to Riverpod (anagram of Provider)!

For learning how to use Riverpod, see its documentation: >>> https://riverpod.dev <<<

Long story short:

  • Define network requests by writing a function annotated with @riverpod:

    @riverpod
    Future<String> boredSuggestion(Ref ref) async {
      final response = await http.get(
        Uri.https('boredapi.com', '/api/activity'),
      );
      final json = jsonDecode(response.body);
      return json['activity']! as String;
    }
    
  • Listen to the network request in your UI and gracefully handle loading/error states.

    class Home extends ConsumerWidget {
      @override
      Widget build(BuildContext context, WidgetRef ref) {
        final boredSuggestion = ref.watch(boredSuggestionProvider);
        // Perform a switch-case on the result to handle loading/error states
        return switch (boredSuggestion) {
          AsyncData(:final value) => Text('data: $value'),
          AsyncError(:final error) => Text('error: $error'),
          _ => const Text('loading'),
        };
      }
    }
    

Contributing

Contributions are welcome!

Here is a curated list of how you can help:

  • Report bugs and scenarios that are difficult to implement
  • Report parts of the documentation that are unclear
  • Fix typos/grammar mistakes
  • Update the documentation or add examples
  • Implement new features by making a pull-request

Sponsors

所属分类

依赖

依赖清单 6 项

源码仓库

使用此插件的项目