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

flutter_offline 封面
套件詳情v6.0.0

flutter_offline

一個整潔的工具,可像老闆一樣處理離線/在線連接。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
5500
喜歡數
680
Pub 分數
140

安裝套件

flutter pub add flutter_offline

套件介紹

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

flutter_offline 是一個輕量且直覺的套件,可簡化 Flutter 應用程式中的網路連線狀態監控。它提供即時的線上與離線狀態偵測,讓開發者能夠建立能順暢適應變動網路狀況的回應式介面。透過最少的設定與跨平台的一致行為,確保在 Android、iOS、Web、macOS、Windows 與 Linux 上實現可靠的連線檢查。此套件可順暢整合至現有應用程式中,提供乾淨的 API,以有效處理網路狀態變更。

適用情境

  • 處理離線資料快取
  • 顯示網絡狀態指示器
  • 啟用離線優先的應用體驗
  • 在連線中斷期間優雅地管理API請求
  • 建構具彈性的使用者介面

主要特色

  • 即時線上/離線狀態檢測
  • 跨平台相容性
  • 簡單而乾淨的 API
  • 自動連接監控
  • 支援行動與桌面平台

適合對象

  • 需要強大網路感知能力的應用程式
  • 離線優先的應用程式開發
  • 面向用戶的連接反饋
  • 依賴網路的業務邏輯
  • 尋求最小整合開銷的開發人員

使用建議

  • 不自動處理重連重試
  • 需要手動實現重試策略
  • 僅限連接狀態,不包含資料同步
  • 沒有內建的離線資料持久化功能
  • 依賴於可能因平台而異的系統級網路API

原始 README

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

✈️ Flutter Offline

Format, Analyze and Test codecov pub package

A tidy utility to handle offline/online connectivity like a Boss. It provides support for all platforms (iOS, Android, Web, macOS, Linux, and Windows).

🎖 Installing

dependencies:
  flutter_offline: "^6.0.0"
⚡️ Import
import 'package:flutter_offline/flutter_offline.dart';
✔ Add Permission to Manifest
<uses-permission android:name="android.permission.INTERNET"/>

🎮 How To Use

import 'package:flutter/material.dart';
import 'package:flutter_offline/flutter_offline.dart';

class DemoPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Offline Demo"),
      ),
      body: OfflineBuilder(
        connectivityBuilder: (
          BuildContext context,
          List<ConnectivityResult> connectivity,
          Widget child,
        ) {
          final bool connected = !connectivity.contains(ConnectivityResult.none);
          return new Stack(
            fit: StackFit.expand,
            children: [
              Positioned(
                height: 24.0,
                left: 0.0,
                right: 0.0,
                child: Container(
                  color: connected ? Color(0xFF00EE44) : Color(0xFFEE4400),
                  child: Center(
                    child: Text("${connected ? 'ONLINE' : 'OFFLINE'}"),
                  ),
                ),
              ),
              Center(
                child: new Text(
                  'Yay!',
                ),
              ),
            ],
          );
        },
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text(
              'There are no bottons to push :)',
            ),
            new Text(
              'Just turn off your internet.',
            ),
          ],
        ),
      ),
    );
  }
}

For more info, please, refer to the main.dart in the example.

🔄 Retry Functionality

Manually retry connectivity checks with exponential backoff:

class MyWidget extends StatefulWidget {
  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  late final OfflineRetryController _retryController;

  @override
  void initState() {
    super.initState();
    _retryController = OfflineRetryController(
      maxRetries: 5,
      retryCooldown: const Duration(seconds: 2),
    );
    _retryController.addListener(() => setState(() {}));
  }

  @override
  void dispose() {
    _retryController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return OfflineBuilder(
      retryController: _retryController,
      connectivityBuilder: (context, connectivity, child) {
        final connected = !connectivity.contains(ConnectivityResult.none);
        return Column(
          children: [
            Text(connected ? 'ONLINE' : 'OFFLINE'),
            if (!connected)
              ElevatedButton(
                onPressed: _retryController.canRetry ? _retryController.retry : null,
                child: Text('Retry (${_retryController.retryCount}/5)'),
              ),
          ],
        );
      },
    );
  }
}
Custom Retry Logic

Override onRetry() or onRetryError() for custom behavior:

class CustomRetryController extends OfflineRetryController {
  CustomRetryController() : super(maxRetries: 3);

  @override
  Future<void> onRetry() async {
    print('Retrying connection...');
  }

  @override
  void onRetryError(Object error, StackTrace stackTrace) {
    print('Retry failed: $error');
  }
}

Features:

  • Exponential backoff (1s, 2s, 4s, 8s, 16s)
  • Configurable retry limits and cooldown
  • Auto-reset on reconnection
  • Extends ChangeNotifier for reactive UI updates

🧪 Testing

Run tests with:

flutter test

📷 Screenshots

🐛 Bugs/Requests

If you encounter any problems feel free to open an issue. If you feel the library is missing a feature, please raise a ticket on Github and I'll look into it. Pull request are also welcome.

❗️ Note

For help getting started with Flutter, view our online documentation.

For help on editing plugin code, view the documentation.

🤓 Mentions

Simon Lightfoot (@slightfoot) is just awesome 👍.

⭐️ License

MIT License

所屬分類

依賴

依賴清單 2 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。