flutter_synckit 0.0.1
flutter_synckit: ^0.0.1 copied to clipboard
A cross-platform multi-device synchronization library
SyncKit #
SyncKit 是一个功能强大的跨平台多端同步库,专为 Flutter 应用设计。它能够帮助开发者快速在不同设备间实现数据同步,支持 WebDAV 和 iCloud(规划中)等多种协议,并提供了包括断点续传、冲突解决、离线支持在内的企业级同步能力。
✨ 核心功能 #
- 多协议支持:内置 WebDAV 支持,可连接 Nextcloud、OwnCloud 或任何标准 WebDAV 服务器。iCloud 同步(iOS/macOS)即将推出。
- 断点续传保护:
- 智能分块:自动将大文件切分为小块上传,节省内存。
- 状态追踪:记录每个分块的上传状态,网络中断后可从断点处继续,无需重传。
- 离线优先 (Offline-First):使用 SQLite 和 SharedPreferences 本地缓存,支持离线操作,网络恢复后自动同步。
- 冲突解决机制:提供智能冲突检测,支持多种解决策略(如保留本地、保留远程、合并等)。
- 数据完整性保障:上传前后进行严格的数据校验,确保文件内容 100% 准确。
📦 安装 #
在你的 pubspec.yaml 文件中添加 synckit:
dependencies:
flutter_synckit: ^1.0.0
然后运行 flutter pub get。
🚀 快速开始 #
1. 初始化 #
在使用 SyncKit 的任何功能之前,必须先进行初始化配置。
import 'package:flutter_synckit/flutter_synckit.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final syncKit = SyncKit();
await syncKit.initialize(
syncProvider: 'webdav', // 目前支持 'webdav'
serverUrl: 'https://dav.jianguoyun.com/dav/', // 你的 WebDAV 服务器地址
username: 'your_email@example.com',
password: 'your_password', // 注意:某些服务商(如坚果云)需要使用应用专用密码
chunkSize: 2 * 1024 * 1024, // 可选:设置分块大小,默认 1MB。这里设置为 2MB
);
runApp(MyApp());
}
2. 上传文件 (支持断点续传) #
SyncKit 会自动处理大文件的分块上传。
import 'dart:io';
Future<void> uploadMyFile() async {
final file = File('/path/to/my/video.mp4');
try {
// 开始上传,返回上传任务信息
final task = await SyncKit().uploadFile(file, remotePath: '/Simulators/video.mp4');
print('上传任务已创建: ${task.id}');
// 监听上传进度
SyncKit().getUploadProgress(task.id).listen((progress) {
print('上传进度: ${(progress.progress * 100).toStringAsFixed(1)}% '
'(${progress.uploaded}/${progress.total} bytes)');
});
} catch (e) {
print('上传出错: $e');
}
}
3. 触发同步 #
手动触发一次数据的同步(包括元数据同步和文件传输)。
await SyncKit().syncData();
4. 下载文件 #
final success = await SyncKit().downloadFile('/remote/path/image.jpg', '/local/path/image.jpg');
if (success) {
print('下载成功');
}
5. 解决同步冲突 #
当本地和云端同时修改了同一个文件时,可以使用 resolveConflict 按照指定策略解决。
// 策略包括:keepLocal, keepRemote, merge, renameLocal, renameRemote
await SyncKit().resolveConflict('item_id_123', ConflictResolutionStrategy.keepLocal);
🛠️ 技术栈 #
- 语言: Flutter (Dart)
- 网络:
http+webdav_client - 本地存储:
sqflite+shared_preferences - 加密/校验:
crypto(SHA-256)
📄 License #
MIT License
Copyright (c) 2026 SyncKit
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.