搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。