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

插件详情v1.1.0

fl_query

用于 Flutter 的异步数据缓存、重新获取和失效库

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
359
喜欢数
38
Pub 分数
130

安装插件

flutter pub add fl_query

插件介绍

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

fl_query 是一个专为管理异步数据而设计的 Flutter 包,内置缓存、自动重新获取和缓存失效功能。它通过高效处理数据获取、加载状态、错误和缓存响应,简化了状态管理。适用于需要实时或频繁更新数据的应用,减少样板代码,并确保应用中数据的一致性。该库可无缝集成到 Flutter 的响应式生态系统中,并支持所有主要平台。

适合场景

  • 实时数据更新
  • 离线优先的应用程序
  • API 数据同步
  • 带有异步数据的动态 UI
  • 缓存 API 响应
  • 自动数据刷新

主要特点

  • 内置缓存
  • 自动重新获取
  • 缓存失效
  • 最小化样板代码
  • 跨平台支持
  • 错误处理

适合对象

  • 使用状态管理的开发者
  • 频繁调用 API 的应用
  • 需要离线功能的项目
  • 注重开发者生产力的团队
  • 具有动态内容的 Flutter 应用

使用建议

  • 需要理解 async/await 模式
  • 使用大缓存时可能会增加内存使用量
  • 最适合中等到高数据频率
  • 对复杂缓存策略的自定义功能有限
  • 不适合简单的静态数据

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

"Fl-Query Logo"

Fl-Query

Asynchronous data caching, refetching & invalidation library for Flutter. FL-Query lets you manage & distribute your async data without touching any global state

Fl-Query makes asynchronous server state management a breeze in flutter

Features

  • Async data caching & management
  • Smart + effective refetching
  • Optimistic updates
  • Automatically cached data invalidation & unneeded query/mutation garbage collection
  • Infinite pagination via InfiniteQuery
  • Lazy persistent cache (Uses hive for persisting query results to disk) (optional)
  • Easy to write & understand code. Follows DRY (Don't repeat yourself) convention
  • Compatible with both vanilla Flutter & elite flutter_hooks

Installation

Regular installation:

$ flutter pub add fl_query

For elite flutter_hooks user (Welcome to the flutter cool community btw😎)

$ flutter pub add flutter_hooks fl_query_hooks

Docs

You can find the documentation of fl-query at https://fl-query.krtirtho.dev/

Basic Usage

Initialize the cache databases in your main method

fl-query uses hive for persisting data to disk

void main()async {
  WidgetsFlutterBinding.ensureInitialized();
  await QueryClient.initialize(cachePrefix: 'fl_query_example');
  runApp(MyApp());
}

In MyApp Widget's build method wrap your MaterialApp with with QueryClientProvider widget

  Widget build(BuildContext context) {
    return QueryClientProvider(
      child: MaterialApp(
        title: 'Fl-Query Example App',
        theme: ThemeData(
          useMaterial3: true,
          primarySwatch: Colors.blue,
        ),
        home: const MyHomePage(),
      ),
    );
  }

Let's write use a Query now FL-Query provides a QueryBuilder widget that creates and listens to the specified Query and re-runs the builder function whenever there's an update

It has 2 required parameters(unnamed) key & builder

class MyApp extends StatelessWidget{
    MyApp({super.key});

    @override
    build(context){
      return QueryBuilder<String, dynamic>(
        'hello',
        () {
          return Future.delayed(
              const Duration(seconds: 6), () => 'Hello World!');
        },
        initial: 'A replacement',
        jsonConfig: JsonConfig(
          fromJson: (json) => json['data'],
          toJson: (data) => {'data': data},
        ),
        onData: (value) {
          debugPrint('onData: $value');
        },
        onError: (error) {
          debugPrint('onError: $error');
        },
        builder: (context, query) {
          if (query.isLoading) {
            return const Center(
              child: CircularProgressIndicator(),
            );
          } else if (query.hasError) {
            return Center(
              child: Text(query.error.toString()),
            );
          }
          return Center(
            child: Text(query.data ?? "Unfortunately, there's no data"),
          );
        },
      );
    }
}

And if you're using flutter_hooks you got that too

class MyApp extends HookWidget{
    MyApp({super.key});

    @override
    build(context){
      final query = useQuery<String, dynamic>(
        'hello',
        () {
          return Future.delayed(
              const Duration(seconds: 6), () => 'Hello World!');
        },
        initial: 'A replacement',
        jsonConfig: JsonConfig(
          fromJson: (json) => json['data'],
          toJson: (data) => {'data': data},
        ),
        onData: (value) {
          debugPrint('onData: $value');
        },
        onError: (error) {
          debugPrint('onError: $error');
        },
      );

      if (query.isLoading) {
        return const Center(
            child: CircularProgressIndicator(),
          );
      } else if (query.hasError) {
        return Center(
          child: Text(query.error.toString()),
        );
      }
      return Center(
        child: Text(query.data ?? "Unfortunately, there's no data"),
      );
    }
}

To master the fl-query follow the official blog at https://fl-query.krtirtho.dev/blog

Why?

The hell, why?

The main purpose of Fl-Query is providing the easiest way to manage the messy server-state part requiring the least amount of code with code reusability & performance This let's you focus more on those cool UI animations & transitions✨. Leave the boring stuff to fl-query

Q. Isn't FutureBuilder good?

No, of course not. Unless you're from 2013 or your app is a purely offline app

Q. So FutureProvider from riverpod or provider not enough?

Probably yes. Although riverpod@v2 has added a lot of caching related features but still optimistic updates, periodic refetching & disk persistence are missing. Let's not forget about infinite pagination, it's a nightmare😅. In case of provider, same story. It's a great package but it's not ideal for server-state management

Notice Board

We need some bada** coders to write tests and ruin our life🥲 100% coverage is our goal🙃

所属分类

依赖

依赖清单 5 项

源码仓库

使用此插件的项目

暂无相关数据。