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

插件详情v4.0.7

riverpod_annotation

一个为 riverpod_generator 暴露注解的包

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
142万
喜欢数
181
Pub 分数
130

安装插件

flutter pub add riverpod_annotation

插件介绍

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

riverpod_annotation 是一个代码生成包,提供注解以简化在 Flutter 应用中创建 Riverpod 提供者的过程。它与 riverpod_generator 配合使用,可自动生成功能代码,减少手动操作和潜在错误。通过使用简单的注解,开发者可以更高效地定义状态管理逻辑,提升代码的可读性和可维护性。该包支持所有主要平台,非常适合跨平台开发。

适合场景

  • 自动化提供者生成
  • 简化状态管理设置
  • 减少 Riverpod 代码中的样板文件
  • 启用类型安全的依赖项
  • 支持大规模应用架构

主要特点

  • 用于定义提供者的注解
  • 与 riverpod_generator 的集成
  • 类型安全强制
  • 跨平台兼容性
  • 最小的运行时开销

适合对象

  • 使用 Riverpod 的 Flutter 开发人员
  • 注重代码可维护性的团队
  • 需要可扩展状态管理的项目
  • 寻求减少样板代码的开发者
  • 面向多个平台的应用程序

使用建议

  • 需要包含 riverpod_generator
  • 需要 build_runner 进行代码生成
  • 可能会略微增加构建时间
  • 不适用于运行时提供程序创建
  • 依赖于正确注解的使用

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

所属分类

依赖

依赖清单 2 项

源码仓库

使用此插件的项目