Back to Blog
Mobile Development

Flutter Offline-First IoT Apps: Hive + Sync Architecture That Works in the Field

Factory floors, remote farms, and oil fields have unreliable connectivity. IoT apps that require internet to show data fail your clients. This guide builds a Flutter offline-first architecture using Hive local storage with intelligent cloud sync.

September 15, 2024
12 min read
FlutterOffline-FirstHiveIoT

Flutter Offline-First IoT Apps: Hive + Sync Architecture That Works in the Field

Here's a scenario that happens constantly: a maintenance technician is inspecting IoT sensors in a basement or a remote site with poor signal. The app spins, shows "No connection", and the technician can't see the last known device state or log their inspection notes.

IoT apps that require internet to display data have failed in their fundamental purpose. The devices exist in the physical world; your app needs to too.

This guide builds a production offline-first Flutter architecture using Hive for local storage and a smart sync queue for cloud reconciliation.

Why Hive for IoT Apps

Hive is a pure Dart NoSQL key-value store — no native code, no SQLite overhead. For IoT apps:

  • Fast: reads are in-memory after initial load
  • Typed: Hive adapters give you type-safe models
  • Portable: works identically on iOS, Android, and Flutter web
  • Simple: no schema migrations, no SQL
  • For relational queries (filter all devices by location), combine Hive with a simple in-memory index or use Isar (Hive's successor with full query support).

    Step 1: Hive Setup

    dependencies:
      hive_flutter: ^1.1.0
      connectivity_plus: ^6.0.0
      riverpod: ^2.5.0

    dev_dependencies: hive_generator: ^2.0.0 build_runner: ^2.4.0

    Define your data models:

    import 'package:hive/hive.dart';

    part 'device_snapshot.g.dart';

    @HiveType(typeId: 0) class DeviceSnapshot extends HiveObject { @HiveField(0) String deviceId; @HiveField(1) double? temperature; @HiveField(2) double? humidity; @HiveField(3) bool isOnline; @HiveField(4) DateTime lastSeen; @HiveField(5) String firmwareVersion; @HiveField(6) String location;

    DeviceSnapshot({ required this.deviceId, this.temperature, this.humidity, required this.isOnline, required this.lastSeen, required this.firmwareVersion, required this.location, }); }

    @HiveType(typeId: 1) class SyncQueueItem extends HiveObject { @HiveField(0) String id; @HiveField(1) String type; // 'command', 'note', 'ack' @HiveField(2) String payload; // JSON @HiveField(3) DateTime createdAt; @HiveField(4) int retryCount; @HiveField(5) bool synced;

    SyncQueueItem({ required this.id, required this.type, required this.payload, required this.createdAt, this.retryCount = 0, this.synced = false }); }

    flutter pub run build_runner build --delete-conflicting-outputs
    

    Initialize Hive in main.dart:

    Future main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await Hive.initFlutter();
      Hive.registerAdapter(DeviceSnapshotAdapter());
      Hive.registerAdapter(SyncQueueItemAdapter());
      await Hive.openBox('devices');
      await Hive.openBox('syncQueue');
      runApp(const ProviderScope(child: MyApp()));
    }
    

    Step 2: Repository Pattern

    Wrap all data access behind a repository — the UI never talks to Hive or the API directly:

    class DeviceRepository {
      final Box _local = Hive.box('devices');
      final ApiService _api;

    DeviceRepository(this._api);

    // Always read local first List getAllDevices() => _local.values.toList();

    DeviceSnapshot? getDevice(String deviceId) => _local.get(deviceId);

    // Update local immediately, queue cloud sync Future updateFromTelemetry(String deviceId, Map data) async { final existing = _local.get(deviceId); final snapshot = DeviceSnapshot( deviceId: deviceId, temperature: data['temperature']?.toDouble(), humidity: data['humidity']?.toDouble(), isOnline: true, lastSeen: DateTime.now(), firmwareVersion: data['firmware'] ?? existing?.firmwareVersion ?? 'unknown', location: existing?.location ?? 'Unknown', ); await _local.put(deviceId, snapshot); }

    // For initial load: fetch from API, cache locally Future refreshFromCloud() async { try { final devices = await _api.getDevices(); for (final device in devices) { await _local.put(device.deviceId, device); } } catch (_) { // Silently fail — we have local data } } }

    Step 3: Connectivity Monitoring

    final connectivityProvider = StreamProvider((ref) {
      return Connectivity().onConnectivityChanged.map(
        (result) => result != ConnectivityResult.none,
      );
    });

    // Trigger sync when connectivity restored final syncOnReconnectProvider = Provider((ref) { ref.listen>(connectivityProvider, (prev, next) { final wasOffline = prev?.value == false; final isNowOnline = next.value == true; if (wasOffline && isNowOnline) { ref.read(syncServiceProvider).processSyncQueue(); } }); });

    Step 4: Sync Queue for Offline Actions

    When offline, user actions (sending a device command, logging an inspection note) go into a sync queue:

    class SyncService {
      final Box _queue = Hive.box('syncQueue');
      final ApiService _api;

    Future enqueue(String type, Map payload) async { final item = SyncQueueItem( id: const Uuid().v4(), type: type, payload: jsonEncode(payload), createdAt: DateTime.now(), ); await _queue.put(item.id, item); }

    Future processSyncQueue() async { final pending = _queue.values.where((i) => !i.synced).toList() ..sort((a, b) => a.createdAt.compareTo(b.createdAt)); // FIFO

    for (final item in pending) { try { await _processItem(item); item.synced = true; await item.save(); } catch (e) { item.retryCount++; await item.save(); if (item.retryCount >= 5) { // Dead letter — notify user await _notifyFailedSync(item); } } } }

    Future _processItem(SyncQueueItem item) async { final payload = jsonDecode(item.payload) as Map; switch (item.type) { case 'command': await _api.sendDeviceCommand(payload['deviceId'], payload['command']); case 'inspection_note': await _api.saveNote(payload); case 'alert_ack': await _api.acknowledgeAlert(payload['alertId']); } } }

    Step 5: Optimistic UI

    Show the user the result of their action immediately — don't wait for cloud confirmation:

    Future sendDeviceCommand(String deviceId, String command) async {
      // 1. Update local state immediately (optimistic)
      final device = _local.get(deviceId)!;
      device.lastCommand = command;
      device.lastCommandAt = DateTime.now();
      await device.save();

    // 2. Attempt cloud sync final isOnline = await Connectivity().checkConnectivity() != ConnectivityResult.none; if (isOnline) { try { await _api.sendDeviceCommand(deviceId, command); } catch (_) { // Fall through to queue } }

    // 3. Queue for sync if offline or API failed await syncService.enqueue('command', {'deviceId': deviceId, 'command': command}); }

    Offline Indicators in the UI

    Always show the user when they're working with cached data:

    class DeviceScreen extends ConsumerWidget {
      @override
      Widget build(BuildContext context, WidgetRef ref) {
        final isOnline = ref.watch(connectivityProvider).value ?? true;
        final device   = ref.watch(deviceProvider(deviceId));

    return Scaffold( appBar: AppBar( title: const Text('Device Monitor'), actions: [ if (!isOnline) const Chip( label: Text('Offline — cached data'), backgroundColor: Colors.orange, ), ], ), body: Column(children: [ if (device.lastSeen.isBefore(DateTime.now().subtract(const Duration(hours: 1)))) const Banner(message: 'Data may be stale', location: BannerLocation.topEnd), DeviceDataPanel(device: device), ]), ); } }

    Stale Data Strategy

    | Data Age | UI Treatment | |----------|--------------| | < 5 min | Show normally | | 5–60 min | Subtle "last seen X ago" label | | 1–24 hr | Yellow warning badge | | > 24 hr | Red staleness warning, prompt refresh |

    IoT apps deployed in factories, farms, and remote installations need to work where connectivity doesn't. [Contact Code Caracal](/contact) to build your offline-first IoT mobile app.

    Written by CodeCaracal Engineering

    We write from production experience — every technique in our articles has been deployed to real clients. No academic theory.

    More Articles

    Business · 12 min read

    IoT Device Compliance: FCC, CE, and Product Certification Guide for Hardware Startups

    Business · 11 min read

    What to Look for When Hiring an IoT Development Partner: 8 Critical Criteria

    Business · 11 min read

    IoT MVP to Production: Realistic Timeline and Budget for Hardware Startups

    Business · 11 min read

    IoT Development Agency vs Building In-House: A Decision Framework for Founders

    IoT Dashboard · 13 min read

    Next.js IoT Analytics Dashboard: From Sensor Data to Production App

    Business · 11 min read

    How Much Does It Cost to Build an IoT Product in 2024? A Realistic Breakdown

    IoT Dashboard · 11 min read

    IoT Dashboard UX: Design Principles for Industrial Monitoring Interfaces

    IoT Dashboard · 12 min read

    Node.js WebSocket Server: The Real-Time Backend for IoT Dashboards

    Cloud & DevOps · 12 min read

    Containerizing IoT Backend Services with Docker: From Dev to Production

    IoT Dashboard · 14 min read

    Grafana + InfluxDB IoT Monitoring: Complete Production Setup Guide

    IoT Dashboard · 12 min read

    Building Real-Time IoT Dashboards with React and Recharts

    Cloud & DevOps · 13 min read

    CI/CD for Embedded Firmware: Automated Build, Test, and OTA Release Pipeline

    Cloud & DevOps · 14 min read

    Terraform for IoT Infrastructure: Provisioning AWS IoT Core, Lambda, and InfluxDB as Code

    Mobile Development · 10 min read

    Flutter IoT Alerts: Firebase Push Notifications for Device Events

    Cloud & DevOps · 12 min read

    Deploying IoT Backends on AWS: ECS Fargate vs Lambda vs EC2 Decision Guide

    Mobile Development · 11 min read

    Flutter + MQTT: Building Production IoT Mobile Apps That Scale

    Mobile Development · 13 min read

    Flutter BLE: Building a Bluetooth IoT Controller App from Scratch

    Cloud & DevOps · 13 min read

    AWS IoT Core vs Azure IoT Hub vs Google Cloud IoT: 2024 Honest Comparison

    IoT Engineering · 13 min read

    Kafka vs RabbitMQ for IoT: Choosing the Right Message Queue for High-Volume Telemetry

    IoT Engineering · 14 min read

    IoT System Testing: Unit, Integration, Hardware-in-the-Loop, and End-to-End

    IoT Engineering · 14 min read

    Predictive Maintenance with IoT Sensor Data: From Threshold to Machine Learning

    Embedded Systems · 14 min read

    IoT Bootloader Design: Secure Boot, A/B Partitions, and Reliable OTA Recovery

    IoT Engineering · 14 min read

    Multi-Tenant IoT Platform Architecture: Isolation, Scaling, and Data Partitioning

    Embedded Systems · 14 min read

    Memory Management in Embedded Firmware: Avoiding Heap Fragmentation and Stack Overflows

    IoT Engineering · 13 min read

    IoT Cost Optimization: How We Cut AWS IoT Bills by 60% Without Sacrificing Reliability

    IoT Engineering · 12 min read

    Edge Computing in IoT: When to Process On-Device vs In the Cloud

    IoT Engineering · 13 min read

    Digital Twins for IoT: Building a Virtual Mirror of Your Physical Devices

    Embedded Systems · 14 min read

    ESP32 Deep Sleep Mastery: Cutting Power Consumption from 240mA to 10µA

    IoT Engineering · 10 min read

    MQTT QoS 0, 1, and 2 Explained: Choosing the Right Level for IoT

    IoT Engineering · 14 min read

    IoT Monitoring and Observability: Metrics, Logs, and Distributed Tracing

    Embedded Systems · 14 min read

    Debugging Embedded Firmware: JTAG, GDB, Logic Analyzers, and Serial Tracing

    IoT Engineering · 12 min read

    WebSocket vs MQTT vs Server-Sent Events: Real-Time IoT Protocol Deep Dive

    Embedded Systems · 13 min read

    STM32 HAL vs Low-Level Drivers: When the Abstraction Costs You Too Much

    IoT Engineering · 13 min read

    IoT Data Pipeline: From Raw Sensor Reading to Live Dashboard in Under 100ms

    IoT Engineering · 13 min read

    Zero-Touch IoT Device Provisioning: Scaling from 10 to 100,000 Devices

    Embedded Systems · 13 min read

    UART vs SPI vs I2C: Choosing the Right Protocol for Sensor Integration

    IoT Engineering · 12 min read

    Real-Time IoT Alerting: From Simple Thresholds to ML Anomaly Detection

    Embedded Systems · 12 min read

    ESP32 Partition Table: Designing Flash Layout for Production Firmware

    IoT Engineering · 12 min read

    IoT Architecture Patterns: Hub-and-Spoke, Mesh, and Edge-Cloud Hybrid

    Embedded Systems · 13 min read

    IoT Battery Life Optimization: Engineering Devices That Last Years on a Single Charge

    IoT Engineering · 13 min read

    Time-Series Databases for IoT: InfluxDB vs TimescaleDB vs AWS Timestream

    Security · 14 min read

    Zero-Trust Security for Embedded IoT: Why Your Devices Are Probably Vulnerable

    Embedded Systems · 14 min read

    FreeRTOS on ESP32: Task Scheduling, Queues, and Resource Management for IoT

    IoT Engineering · 12 min read

    Building a Production IoT Gateway with Raspberry Pi and Node.js

    Embedded Systems · 13 min read

    ESP32 vs STM32: Choosing the Right Microcontroller for Your IoT Project

    Mobile Development · 10 min read

    Flutter + WebSocket: Building Real-Time IoT Dashboards That Don't Stutter

    IoT Engineering · 13 min read

    IoT Fleet Management at Scale: AWS IoT Core Device Registry and Provisioning

    IoT Engineering · 11 min read

    MQTT vs HTTP for IoT: Which Protocol Wins in Production?

    IoT Engineering · 12 min read

    ESP32 → MQTT → AWS IoT Core: The Production-Grade Architecture Guide

    Let's Build Together

    Got an IoT challenge?
    We've shipped it.

    Whether you need a fleet to track, a factory to monitor, or a farm to automate — our team has done it before and we'd love to build it with you. Typical response time: under 24 hours.

    No upfront commitment99.9% uptime SLANDA on requestFixed-price options