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

An implementation to save big chunks of content to a file

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter_key_value_file_storage/flutter_key_value_file_storage.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';

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

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _content = '';
  String _loadedContent = '';
  String _key = '';
  bool _loading = false;
  late final FlutterKeyValueFileStorage _fileStorage;

  @override
  void initState() {
    super.initState();
    _fileStorage = FlutterKeyValueFileStorage(const FlutterSecureStorage());
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Flutter file storage app'),
        ),
        body: SafeArea(
          child: Padding(
            padding: const EdgeInsets.all(8),
            child: Column(
              children: [
                TextField(
                  decoration: const InputDecoration(
                    labelText: 'Key',
                  ),
                  onChanged: (value) {
                    setState(() {
                      _key = value;
                    });
                  },
                ),
                TextField(
                  decoration: const InputDecoration(
                    labelText: 'Content',
                  ),
                  onChanged: (value) {
                    setState(() {
                      _content = value;
                    });
                  },
                ),
                MaterialButton(
                  child: const Text('Save'),
                  onPressed: () async {
                    setState(() {
                      _loading = true;
                    });
                    await _fileStorage.write(key: _key, value: _content);
                    setState(() {
                      _loading = false;
                    });
                  },
                ),
                MaterialButton(
                  child: const Text('Load from key'),
                  onPressed: () async {
                    setState(() {
                      _loading = true;
                    });
                    final content = await _fileStorage.read<String>(key: _key);
                    setState(() {
                      _loadedContent = content ?? '';
                      _loading = false;
                    });
                  },
                ),
                if (_loading) ...[
                  const CircularProgressIndicator(),
                ] else ...[
                  Text('Content from file: $_loadedContent'),
                ],
              ],
            ),
          ),
        ),
      ),
    );
  }
}