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

插件详情v4.0.9

riverpod_generator

Riverpod 的代码生成器。它既简化了语法,又增强了功能,例如支持有状态热重载。

AndroidiOSmacOSWindowsLinux
近 30 天下载
114万
喜欢数
437
Pub 分数
130

安装插件

flutter pub add riverpod_generator

插件介绍

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

Riverpod_generator 是 Flutter 中 Riverpod 状态管理库的代码生成工具。它通过自动生成提供者定义来简化样板代码,实现更简洁的语法和更出色的开发体验。该生成器支持有状态热重载,允许在开发过程中保持状态而无需重启。它与 Riverpod 生态系统无缝集成,减少手动编码并提高可维护性。专为性能和易用性设计,帮助开发者专注于逻辑而非重复的设置。

适合场景

  • 简化复杂的提供者声明
  • 在开发中启用有状态热重载
  • 减少大型应用中的样板代码
  • 跨多个文件自动创建提供者
  • 提高代码的可读性和可维护性

主要特点

  • 自动提供者代码生成
  • 支持有状态热重载
  • 减少手动样板代码
  • 与 Riverpod 的 API 集成
  • 生成类型安全的代码

适合对象

  • 使用 Riverpod 的 Flutter 开发人员
  • 构建可扩展状态管理应用的团队
  • 寻求更快迭代体验的开发者可使用热重载功能
  • 需要干净、可维护的状态逻辑的项目
  • 希望减少重复的提供者设置的人

使用建议

  • 需要 Dart 代码生成设置
  • 需要正确的 build_runner 配置
  • 可能会略微增加构建时间
  • 依赖 Riverpod 核心包
  • 对于简单、手写的提供者不需要

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

所属分类

依赖

依赖清单 12 项

源码仓库

使用此插件的项目