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

套件詳情v1.0.0+4

ready_view

在使用 Flutter 開發時,通常需要在從網路或資料庫取得資訊時使用非同步操作。在這種情況下,我們需要在頁面上包含載入元件,追蹤載入完成狀態,並在載入完成後顯示原始檢視。包含這一系列程式碼會使檢視程式碼變得相當複雜且混亂。因此,ready_view 庫提供了一種簡單而優雅的解決方案,用於處理非同步動作。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
2
喜歡數
4
Pub 分數
145

安裝套件

flutter pub add ready_view

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

ReadyView 是一個專為 Flutter/Dart 設計的套件,旨在簡化 UI 開發中的異步操作處理。它提供了一種乾淨、聲明式的辦法來管理從網路或資料庫取得資料時的載入狀態、成功結果與錯誤。透過以 ReadyView 包覆內容,開發者可以避免撰寫用於載入指示器與狀態追蹤的重複程式碼,進而獲得更清晰、更易維護的元件樹。此套件支援所有主要平台,並能順暢整合至現有的 Flutter 專案中。

適用情境

  • 處理 API 回應
  • 顯示加載狀態
  • 管理錯誤條件
  • 簡化非同步 UI 邏輯
  • 取代手動狀態管理

主要特色

  • 宣告式載入狀態
  • 內建錯誤處理
  • 最小化樣板程式碼
  • 跨平台支援
  • 輕鬆整合基於 Future 的作業

適合對象

  • 尋求乾淨的異步UI模式的開發者
  • 頻繁進行網路/資料庫呼叫的專案
  • 希望減少小部件複雜性的團隊
  • Flutter 初學者學習非同步工作流程
  • 需要響應式加載反饋的應用程式

使用建議

  • 需要理解期貨
  • 僅限非同步操作包裝器
  • 不適合複雜的狀態機
  • 可能需要額外的邏輯來實現重試機制
  • 最適合用於簡單的取得與顯示情境

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

pub package GitHub

ready_view

Motivation

When developing with Flutter, it is often necessary to use asynchronous operations when fetching information from networks or databases. In such cases, we need to include loading widgets on our pages, track the completion of loading, and display the original view once the loading is complete. Including this series of code can make the view code quite complex and messy.

Therefore, the ready_view library offers a simple and elegant solution for handling asynchronous actions.

Installation

flutter pub add ready_view

Usage

1. Change Code
When Stateless Widget
// replace
StatelessWidget => ReadyStatelessWidget

build(BuildContext context) => buildWhenReady(BuildContext context)
Stateful Widget
// replace
extends State<.. => extends ReadyState<..

build(BuildContext context) => buildWhenReady(BuildContext context)
2. Add readyState(Optional)

When asynchronous actions are required, you can override the readyState() to perform the necessary tasks.

// handle async operation here if needed
@override
Function? readyState(BuildContext context) {
  return () async {
    // your async code here
    await Future.delayed(const Duration(seconds: 2));
  };
}
3. Custom Loading Widget(Optional)

You can customize the loading widget by overriding the loadingWidget() if you want.

  // custom loading widget if needed
  @override
  Widget loadingWidget() {
    return Scaffold(
        body: Center(
            child: Container(
      width: 100,
      height: 100,
      color: Colors.red,
    )));
  }

Example

Stateless Widget
import 'package:flutter/material.dart';
import 'package:ready_view/ready_view.dart';

class StatelessExample extends ReadyStatelessWidget {
  const StatelessExample({super.key});

  // handle async operation here if needed
  @override
  Function? readyState(BuildContext context) {
    return () async {
      await Future.delayed(const Duration(seconds: 2));
    };
  }

  // custom loading widget if needed
  // @override
  // Widget loadingWidget() {
  //   return Scaffold(
  //       body: Center(
  //           child: Container(
  //     width: 100,
  //     height: 100,
  //     color: Colors.red,
  //   )));
  // }

  @override
  Widget buildWhenReady(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('StatelessExample'),
      ),
      body: const Center(
        child: Text('Content is ready!'),
      ),
    );
  }
}

void main() {
  runApp(const MaterialApp(
    home: StatelessExample(),
  ));
}
Stateful Widget
import 'package:flutter/material.dart';
import 'package:ready_view/ready_view.dart';

class StatefulExample extends StatefulWidget {
  const StatefulExample({super.key});

  @override
  ReadyState<StatefulExample> createState() => _StatefulExampleState();
}

class _StatefulExampleState extends ReadyState<StatefulExample> {
  
  // handle async operation here if needed
  @override
  Function? readyState(BuildContext context) {
    return () async {
      await Future.delayed(const Duration(seconds: 2));
    };
  }

  // custom loading widget if needed
  // @override
  // Widget loadingWidget() {
  //   return Scaffold(
  //       body: Center(
  //           child: Container(
  //     width: 100,
  //     height: 100,
  //     color: Colors.red,
  //   )));
  // }

  @override
  Widget buildWhenReady(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('StatefulExample'),
      ),
      body: const Center(
        child: Text('Content is ready!'),
      ),
    );
  }
}

void main() {
  runApp(const MaterialApp(
    home: StatefulExample(),
  ));
}
Comparison with FutureBuilder

Although similar to FutureBuilder, using FutureBuilder asynchronously when the view is first created requires setting up related state, including calling asynchronous functions in initState, and placing the FutureBuilder in the build function along with a separate loading widget. In contrast, ready_view allows you to keep your existing code as it is, while providing a cleaner and more intuitive way to handle asynchronous actions by separately calling the readyState and loadingWidget functions when needed.

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。