搜索插件、代码仓与 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.

所属分类

源码仓库

使用此插件的项目

暂无相关数据。