Mastering Secure Offline Data Sync in Flutter: Boost Reliability & User Experience
DEV Community

Mastering Secure Offline Data Sync in Flutter: Boost Reliability & User Experience

Introduction & The Problem

Modern mobile applications are expected to function flawlessly, even when network connectivity is unreliable or completely absent.

Imagine a field service technician needing to access crucial client information or log work orders in a remote area without cellular service. Or a retail employee processing sales transactions during a network outage.

In these scenarios, the inability to operate offline not only frustrates users but can lead to significant business losses, inaccurate data, and operational inefficiencies.

The core problem isn't just about saving data locally; it's about securely synchronizing that data with a remote server once connectivity is restored, without conflicts or data corruption.

Developers often face challenges with:

  • Data Consistency: Ensuring local and remote datasets remain aligned.
  • Conflict Resolution: Handling simultaneous modifications to the same data item.
  • Performance: Efficiently storing and retrieving large volumes of data on the device.
  • Security: Protecting sensitive data both at rest on the device and during transmission.
  • User Experience: Providing seamless offline functionality without manual intervention.

Leaving these issues unresolved leads to disgruntled users, costly support tickets, and ultimately, a less reliable application that fails to meet critical business needs.

The Solution Concept & Architecture

Our solution involves building a robust offline-first architecture for Flutter applications. This design prioritizes local data access and ensures data integrity through a carefully managed synchronization process.

We'll leverage a high-performance local database and a dedicated synchronization service.

Core Architectural Components:

  • Local Database: A fast, embedded NoSQL database for storing application data directly on the device. For this guide, we'll use Isar Database, known for its performance and ease of use in Flutter.
  • Repository Pattern: An abstraction layer that decouples the application logic from the data sources (local database and remote API). This provides a clean interface for data operations.
  • Synchronization Service: A dedicated service responsible for detecting local changes, pushing them to the remote server, pulling down remote updates, and resolving any conflicts. This service will operate intelligently in the background when network connectivity is available.
  • Remote API: Your existing backend API that provides the authoritative data source.

Data Flow:

The UI interacts exclusively with the Repository. The Repository first attempts to retrieve data from the Local Database.

When data is modified locally, the Repository saves it to the Local Database and marks it as 'dirty' (pending synchronization).

The Synchronization Service periodically checks for 'dirty' data and initiates a sync process when the network is available.

During synchronization, local changes are uploaded, remote updates are downloaded, and conflicts are resolved before updating the Local Database.

Step-by-Step Implementation

Let's walk through the implementation using Isar as our local database.

First, add the necessary dependencies to your pubspec.yaml:

dependencies:
  flutter:
    sdk: flutter
  isar:
  isar_flutter_libs: # required for mobile apps
  path_provider:
  connectivity_plus:
dev_dependencies:
  flutter_test:
    sdk: flutter
  isar_generator:
  build_runner:

Run flutter pub get after adding these.

Define Your Data Model

We'll create a simple Task model. Notice the @collection annotation for Isar, and the isDirty flag to track pending changes, along with remoteId for server identification.

import 'package:isar/isar.dart';
part 'task.g.dart';
@collection
class Task {
  Id id = Isar.autoIncrement; // Local ID for Isar
  String? remoteId; // ID from the backend, null if not yet synced
  late String title;
  late String description;
  late bool isCompleted;
  bool isDirty = true; // Flag for pending sync
  Task(
    {
      this.remoteId,
      required this.title,
      required this.description,
      this.isCompleted = false,
    }
  );
  // Convert Task to a Map for API requests
  Map toJson() {
    return {
      'id': remoteId, // Use remoteId for backend
      'title': title,
      'description': description,
      'isCompleted': isCompleted,
    };
  }
  // Create Task from a JSON Map
  factory Task.fromJson(Map json) {
    return Task(
      remoteId: json['id'] as String?,
      title: json['title'] as String,
      description: json['description'] as String,
      isCompleted: json['isCompleted'] as bool,
    );
  }
}

Run flutter pub run build_runner build to generate task.g.dart.

Initialize Isar Database

A simple service to manage Isar initialization and access.

import 'package:isar/isar.dart';
import 'package:path_provider/path_provider.dart';
import 'task.g.dart'; // Generated file
class IsarService {
  late Future<Isar> db;
  IsarService() {
    db = openIsar();
  }
  Future<Isar> openIsar() async {
    if (Isar.instanceNames.isEmpty) {
      final dir = await getApplicationDocumentsDirectory();
      return await Isar.open(
        [TaskSchema], // Include all your schemas here
        directory: dir.path,
        inspector: true,
      );
    }
    return Future.value(Isar.getInstance());
  }
  Future<List<Task>> getAllTasks() async {
    final isar = await db;
    return await isar.tasks.where().findAll();
  }
  Future<void> saveTask(Task task) async {
    final isar = await db;
    await isar.writeTxn(() async {
      await isar.tasks.put(task); // Insert or update
    });
  }
  Future<void> deleteTask(int id) async {
    final isar = await db;
    await isar.writeTxn(() async {
      await isar.tasks.delete(id);
    });
  }
}

Implement the Repository

The repository acts as the single source of truth for your data.

import 'package:your_app/data/models/task.dart';
import 'package:your_app/data/services/isar_service.dart';
import 'package:your_app/data/services/api_service.dart'; // Assume you have an API service
class TaskRepository {
  final IsarService _isarService;
  final ApiService _apiService;
  TaskRepository(this._isarService, this._apiService);
  Stream<List<Task>> watchAllTasks() async* {
    final isar = await _isarService.db;
    yield* isar.tasks.where().watch(fireImmediately: true);
  }
  Future<List<Task>> getTasks() async {
    return await _isarService.getAllTasks();
  }
  Future<void> addTask(Task task) async {
    task.isDirty = true;
    await _isarService.saveTask(task); // The sync service will pick this up later
  }
  Future<void> updateTask(Task task) async {
    task.isDirty = true;
    await _isarService.saveTask(task); // The sync service will pick this up later
  }
  Future<void> deleteTask(int id) async {
    // Mark as dirty for deletion sync, or directly delete if backend doesn't need to know about local deletes
    // For simplicity, we'll assume a direct delete for now.
    await _isarService.deleteTask(id);
  }
}

Create the Synchronization Service

This service manages the actual sync logic. For background tasks, you might integrate with workmanager or background_fetch for production, but we'll show a simplified foreground trigger here.

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:your_app/data/models/task.dart';
import 'package:your_app/data/services/isar_service.dart';
import 'package:your_app/data/services/api_service.dart';
import 'package:isar/isar.dart';
class SyncService {
  final IsarService _isarService;
  final ApiService _apiService;
  SyncService(this._isarService, this._apiService);
  Future<void> synchronizeData() async {
    var connectivityResult = await (Connectivity().checkConnectivity());
    if (connectivityResult == ConnectivityResult.none) {
      print('No internet connection. Skipping sync.');
      return;
    }
    print('Starting data synchronization...');
    final isar = await _isarService.db;
    await isar.writeTxn(() async {
      // 1. Upload local changes (dirty tasks)
      final dirtyTasks = await isar.tasks.filter().isDirtyEqualTo(true).findAll();
      for (var task in dirtyTasks) {
        try {
          if (task.remoteId == null) {
            // New task, create on backend
            final createdTask = await _apiService.createTask(task.toJson());
            task.remoteId = createdTask['id'] as String;
          } else {
            // Existing task, update on backend
            await _apiService.updateTask(task.remoteId!, task.toJson());
          }
          task.isDirty = false;
          await isar.tasks.put(task); // Mark as clean
        } catch (e) {
          print('Error uploading task ${task.id}: $e'); // Implement retry logic or error reporting
        }
      }
      // 2. Download remote changes and resolve conflicts
      try {
        final remoteTasksData = await _apiService.getTasks();
        final remoteTasks = remoteTasksData.map((json) => Task.fromJson(json)).toList();
        for (var remoteTask in remoteTasks) {
          final localTask = await isar.tasks.filter().remoteIdEqualTo(remoteTask.remoteId).findFirst();
          if (localTask == null) {
            // New task from backend, insert locally
            remoteTask.isDirty = false; // It's clean from server
            await isar.tasks.put(remoteTask);
          } else if (localTask.isDirty!) {
            // Conflict: local changes exist and remote has updated.
            // Conflict resolution strategy: 'Last Write Wins' (or implement custom merge)
            // For simplicity, we'll let the local dirty changes be uploaded later and potentially overwrite.
            // A more robust strategy might compare timestamps or merge fields.
            print('Conflict detected for task ${localTask.remoteId}. Local changes will be prioritized (LWW).');
          } else {
            // Local task is clean, update with remote changes
            localTask.title = remoteTask.title;
            localTask.description = remoteTask.description;
            localTask.isCompleted = remoteTask.isCompleted;
            await isar.tasks.put(localTask);
          }
        }
      } catch (e) {
        print('Error downloading tasks: $e');
      }
    });
    print('Data synchronization complete.');
  }
}

You'll need a placeholder ApiService for this example. Here's a very basic one:

import 'dart:convert';
import 'package:http/http.dart' as http;
class ApiService {
  final String baseUrl = 'https://your-backend-api.com/api'; // Replace with your actual API base URL
  Future<List<Map<String, dynamic>>> getTasks() async {
    final response = await http.get(Uri.parse('$baseUrl/tasks'));
    if (response.statusCode == 200) {
      return List<Map<String, dynamic>>.from(json.decode(response.body));
    } else {
      throw Exception('Failed to load tasks');
    }
  }
  Future<Map<String, dynamic>> createTask(Map<String, dynamic> taskData) async {
    final response = await http.post(
      Uri.parse('$baseUrl/tasks'),
      headers: {'Content-Type': 'application/json'},
      body: json.encode(taskData),
    );
    if (response.statusCode == 201) {
      return json.decode(response.body);
    } else {
      throw Exception('Failed to create task');
    }
  }
  Future<void> updateTask(String id, Map<String, dynamic> taskData) async {
    final response = await http.put(
      Uri.parse('$baseUrl/tasks/$id'),
      headers: {'Content-Type': 'application/json'},
      body: json.encode(taskData),
    );
    if (response.statusCode != 200) {
      throw Exception('Failed to update task');
    }
  }
}

Integrating Sync into Your App

You can trigger the sync service periodically or when the app comes online. For instance, in your main application widget:

import 'package:flutter/material.dart';
import 'package:your_app/data/services/api_service.dart';
import 'package:your_app/data/services/isar_service.dart';
import 'package:your_app/data/services/sync_service.dart';
import 'package:your_app/data/repositories/task_repository.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final isarService = IsarService();
  final apiService = ApiService();
  final taskRepository = TaskRepository(isarService, apiService);
  final syncService = SyncService(
    isarService,
    apiService,
  );
  // Initial sync on app start
  await syncService.synchronizeData();
  runApp(MyApp(taskRepository: taskRepository, syncService: syncService));
}
class MyApp extends StatefulWidget {
  final TaskRepository taskRepository;
  final SyncService syncService;
  const MyApp({
    Key? key,
    required this.taskRepository,
    required this.syncService,
  }) : super(key: key);
  @override
  State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
  late Stream<ConnectivityResult> _connectivityStream;
  @override
  void initState() {
    super.initState();
    _connectivityStream = Connectivity().onConnectivityChanged;
    _connectivityStream.listen((ConnectivityResult result) {
      if (result != ConnectivityResult.none) {
        // Trigger sync when connectivity is restored
        widget.syncService.synchronizeData();
      }
    });
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Offline-First App',
      home: Scaffold(
        appBar: AppBar(title: const Text('Tasks')),
        body: Center(
          child: StreamBuilder<List<Task>>(
            stream: widget.taskRepository.watchAllTasks(),
            builder: (context, snapshot) {
              if (snapshot.connectionState == ConnectionState.waiting) {
                return const CircularProgressIndicator();
              } else if (snapshot.hasError) {
                return Text('Error: ${snapshot.error}');
              } else if (!snapshot.hasData || snapshot.data!.isEmpty) {
                return const Text('No tasks yet. Add one!');
              } else {
                final tasks = snapshot.data!;
                return ListView.builder(
                  itemCount: tasks.length,
                  itemBuilder: (context, index) {
                    final task = tasks[index];
                    return ListTile(
                      title: Text(task.title),
                      subtitle: Text(
                        task.description + (task.isDirty ? ' (Pending Sync)' : ''),
                      ),
                      trailing: Checkbox(
                        value: task.isCompleted,
                        onChanged: (bool? value) {
                          if (value != null) {
                            final updatedTask =
Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.