ma_ng_outbox 0.0.8 copy "ma_ng_outbox: ^0.0.8" to clipboard
ma_ng_outbox: ^0.0.8 copied to clipboard

unlisted

Mariapps Internal Package to build Flutter apps faster

๐Ÿ“ฆ Offline Outbox Sync for Flutter (ObjectBox + Isolate + Retry + Multipart Support)

A powerful and lightweight offline-first outbox queue system for Flutter apps. Designed for real-world enterprise use cases where network instability, large payloads, and token expiry must be handled automatically.

Supports:

โœ… JSON API requests

โœ… Multipart file uploads

โœ… Multiple files per request

โœ… Empty multipart field names

โœ… Automatic retry with priority

โœ… Automatic token refresh (401 handling)

โœ… Runs in Isolate for non-blocking UI

โœ… ObjectBox storage for fast persistence

โœ… Fully background-safe

๐Ÿš€ Features Feature Status JSON request outbox โœ… Multipart upload (files/images/docs) โœ… Multiple files with dynamic field names โœ… Empty file field name ("") support โœ… Priority-based queue (1 โ†’ 4) โœ… Automatic retry on failures โœ… Retry with new token on 401 โœ… Save status code + response โœ… RootIsolateToken support โœ… Zero UI freeze Isolate-based Production-ready ๐Ÿ”ฅ ๐Ÿ“ฆ Installation

Add to pubspec.yaml:

dependencies: ma_ng_outbox: git: url: https://github.com/

Make sure ObjectBox is installed:

dependencies: objectbox: any objectbox_flutter_libs: any

Run:

flutter pub get

๐Ÿ›  Basic Setup 1๏ธโƒฃ Initialize Outbox in main.dart void main() async { WidgetsFlutterBinding.ensureInitialized();

await OutboxBootstrap.setup();

runApp(MyApp()); }

๐Ÿงฑ How Outbox Works

Every API call is stored as an OutboxItem:

await objectBox.addToOutbox( operation: "POST", url: "https://api.server.com/save", payload: {"title": "Offline Save"}, priority: Priority.high, primaryKey: "local-123", );

Then Outbox will:

โœ” Automatically sync when online โœ” Retry failed requests โœ” Retry 401 using refresh token โœ” Save response & status code โœ” Process highest priority first ๐Ÿงต Architecture Overview UI Layer โ”€โ”€โ”€โ”€โ”€โ–บ addToOutbox() โ”€โ”€โ”€โ”€โ”€โ”€โ–บ ObjectBox storage โ”‚ โ–ผ Background Sync Trigger โ”‚ โ–ผ Isolate Spawned โ”‚ โ–ผ outboxIsolateEntry() reads pending items โ”‚ โ”œโ”€โ”€โ–บ JSON Request โ”œโ”€โ”€โ–บ Multipart Request โ”œโ”€โ”€โ–บ Multi-file Upload โ””โ”€โ”€โ–บ Retry on 401 w/ Refresh Token โ”‚ โ–ผ Main Isolate receives result โ”‚ โ–ผ Update ObjectBox (status + response)

๐Ÿ“ก Adding JSON Requests await objectBox.addToOutbox( operation: "POST", url: Api.saveData, payload: { "name": "Offline Entry", "timestamp": DateTime.now().toString(), }, priority: Priority.medium, );

๐Ÿ“ Adding File Upload Requests 1 file await objectBox.addToOutbox( operation: "POST", url: Api.uploadImage, payload: { "description": "Offline photo", "userId": 12, }, filePathsJson: jsonEncode([imageFile.path]), fileFieldsJson: jsonEncode(["file"]), // or "" );

Multiple files await objectBox.addToOutbox( operation: "POST", url: Api.uploadDocuments, payload: {"caseId": 55}, filePathsJson: jsonEncode([frontPath, backPath]), fileFieldsJson: jsonEncode(["front", "back"]), );

๐Ÿ” Token Refresh Flow (401 Handling)

If API returns 401 Unauthorized:

Call refresh token endpoint

Save new accessToken into OutboxItem

Retry original request

Continue syncing

This is built-in automatically.

๐Ÿ“ฆ OutboxItem Fields class OutboxItem { int id; String operation; String url; int priority; // 1โ€“4 String? payload; // JSON body String? response; // Server response String? filePathsJson; // ["path1","path2"] String? fileFieldsJson;// ["file",""] int isSynced; // 0 or 1 int retryCount; int lastTried; int createdAt; int? statusCode; // HTTP code String? newAccessToken; }

๐Ÿ”„ Manual Triggering of Sync

If internet becomes available:

SyncController.runIsolateSync( objectBox: objectBox, token: "

๐ŸŒ Network Listener Example ConnectivityService().onNetworkChange.listen((online) { if (online) SyncController.runIsolateSync(...); });

๐Ÿงช Debugging

Enable console logs:

kDebugMode ? print("...") : null;

View saved data using ObjectBox Admin:

if (Admin.isAvailable()) { Admin(objectBox.store); }

๐Ÿš€ Performance & Scaling

25,000+ outbox items tested

Large file uploads supported

No UI freeze due to isolate-based processing

Suitable for enterprise offline apps