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

插件详情v3.5.0

riverpod

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

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
449万
喜欢数
4040
Pub 分数
160

安装插件

flutter pub add riverpod

插件介绍

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

Riverpod 是一个用于 Flutter 的响应式状态管理和依赖注入框架,可简化异步操作和状态更新的处理。它提供了一种清晰、可扩展的方式来管理应用状态,具有强类型、可测试性和高效的响应性。Riverpod 可以对依赖项进行细粒度控制,并避免了旧版状态管理解决方案(如 Provider)常见的陷阱。其缓存机制通过避免重复计算来提升性能。

适合场景

  • 管理全局应用状态
  • 处理异步数据加载
  • 大型应用中的依赖注入
  • 测试有状态逻辑
  • 响应式 UI 更新
  • 在小部件之间共享数据

主要特点

  • 响应式状态更新
  • 内置异步支持
  • 细粒度依赖控制
  • 强大的类型安全性
  • 轻松测试
  • 用于性能的缓存

适合对象

  • 大规模 Flutter 应用
  • 注重可维护性的团队
  • 需要强大异步处理功能的开发者
  • 需要模块化架构的应用程序
  • 强调可测试性的项目

使用建议

  • 初学者的学习曲线更陡峭
  • 需要理解响应式编程
  • 可能在简单应用中引入开销
  • 与较旧的替代方案相比,文档有限

原始 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

所属分类

依赖

依赖清单 9 项

源码仓库

使用此插件的项目

所在榜单