搜尋套件、儲存庫與 APP 分類

套件詳情v3.4.3

hooks_riverpod

一個反應式快取與資料繫結框架。Riverpod 讓處理非同步程式碼變得輕鬆自在。

AndroidiOSmacOSWindowsLinux
近 30 天下載
65.9萬
喜歡數
931
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

所屬分類

依賴

依賴清單 5 項

原始碼儲存庫

使用此套件的專案