icloud_kv_storage_spm 0.1.1 copy "icloud_kv_storage_spm: ^0.1.1" to clipboard
icloud_kv_storage_spm: ^0.1.1 copied to clipboard

PlatformiOSmacOS

Synchronize lightweight key-value data across iOS and macOS devices with iCloud.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:icloud_kv_storage_spm/icloud_kv_storage.dart';

void main() {
  runApp(const ExampleApp());
}

class ExampleApp extends StatelessWidget {
  const ExampleApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'iCloud KV Storage',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
      ),
      home: const CounterPage(),
    );
  }
}

class CounterPage extends StatefulWidget {
  const CounterPage({super.key});

  @override
  State<CounterPage> createState() => _CounterPageState();
}

class _CounterPageState extends State<CounterPage> {
  static const _key = 'counter';
  static const _storage = CKKVStorage();

  int _counter = 0;

  @override
  void initState() {
    super.initState();
    _storage.setCloudKitUpdateHandler(_handleCloudUpdate);
    _loadCounter();
  }

  Future<void> _loadCounter() async {
    final value = await _storage.getString(_key);
    final counter = int.tryParse(value ?? '');
    if (mounted && counter != null) {
      setState(() => _counter = counter);
    }
  }

  void _handleCloudUpdate(Map<String, dynamic> values) {
    final value = values[_storage.getRealKey(_key)];
    final counter = value is String ? int.tryParse(value) : null;
    if (!mounted) {
      return;
    }
    setState(() => _counter = counter ?? 0);
  }

  Future<void> _incrementCounter() async {
    final nextCounter = _counter + 1;
    await _storage.writeString(key: _key, value: '$nextCounter');
    if (mounted) {
      setState(() => _counter = nextCounter);
    }
  }

  Future<void> _clearCounter() async {
    await _storage.delete(_key);
    if (mounted) {
      setState(() => _counter = 0);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('iCloud KV Storage')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text('Synchronized counter'),
            Text(
              '$_counter',
              key: const ValueKey('counter'),
              style: Theme.of(context).textTheme.displaySmall,
            ),
          ],
        ),
      ),
      floatingActionButton: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: <Widget>[
          FloatingActionButton(
            heroTag: 'clear',
            onPressed: _clearCounter,
            tooltip: 'Clear',
            child: const Icon(Icons.delete_outline),
          ),
          const SizedBox(width: 12),
          FloatingActionButton(
            heroTag: 'increment',
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: const Icon(Icons.add),
          ),
        ],
      ),
    );
  }
}