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

插件详情v3.5.0

flutter_riverpod

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

AndroidiOSmacOSWindowsLinux
近 30 天下载
437万
喜欢数
2920
Pub 分数
140

安装插件

flutter pub add flutter_riverpod

插件介绍

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

Flutter Riverpod 是一个功能强大的响应式状态管理和依赖注入框架,专为 Flutter 设计。它简化了异步操作的处理,并通过内置缓存提供高效的数据显示绑定。Riverpod 将逻辑与组件解耦,从而提升可测试性和可维护性。其灵活的提供者系统支持多种数据类型和生命周期管理,非常适合可扩展的应用程序。凭借对所有主要平台的强大支持,Riverpod 提升了开发效率和应用性能。

适合场景

  • 管理复杂的应用程序状态
  • 处理异步数据流
  • 大型应用中的依赖注入
  • 测试有状态逻辑
  • 响应式 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

所属分类

依赖

依赖清单 5 项

源码仓库

使用此插件的项目