save_to_cache 1.0.1 copy "save_to_cache: ^1.0.1" to clipboard
save_to_cache: ^1.0.1 copied to clipboard

This package is used to cache files, can save files individually and in folders.

example/lib/main.dart

import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:save_to_cache/save_to_cache.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Save To Cache Example',
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  File fileSelect = File('');
  String nameFileSelect = 'file-select.example';
  String pathSave = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Save To Cache Example'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          TextButton(
            onPressed: _onSelectFile,
            child: const Text('Select File'),
          ),
          Text(nameFileSelect),
          const Divider(),
          TextButton(
            onPressed: () => _onSaveFileToCache(),
            child: const Text('Save file to cache'),
          ),
          TextButton(
            onPressed: () => _onSaveFileToCache(name: 'save new'),
            child: const Text('Save file to cache with name'),
          ),
          TextButton(
            onPressed: () => _onSaveFileWithFolder(),
            child: const Text('Save file with folder'),
          ),
          TextButton(
            onPressed: () => _onSaveFiles(),
            child: const Text('Save files'),
          ),
          TextButton(
            onPressed: () => _onSaveFilesWithFolder(),
            child: const Text('Save files with folder'),
          ),
          Text(pathSave)
        ],
      ),
    );
  }

  Future<void> _onSelectFile() async {
    FilePickerResult? result = await FilePicker.platform.pickFiles();
    if (result != null) {
      fileSelect = File(result.files.single.path!);
      setState(() => nameFileSelect = result.files.single.name);
    }
  }

  void _showMessage(String message) {
    ScaffoldMessenger.of(context)
      ..hideCurrentSnackBar()
      ..showSnackBar(
        SnackBar(
          content: Text(message),
        ),
      );
  }

  Future<void> _onSaveFileToCache({String? name}) async {
    var result = await SaveToCache.saveFile(file: fileSelect, name: name);
    _showMessage('Successful! $result');
  }

  Future<void> _onSaveFileWithFolder() async {
    var result = await SaveToCache.saveFileWithFolder(
        file: fileSelect, folderName: 'test_folder');
    _showMessage('Successful! $result');
  }

  _onSaveFiles() async {
    var result = await SaveToCache.saveFiles(
      files: [fileSelect],
      //names: ['test1']
    );
    _showMessage('Successful! $result');
  }

  _onSaveFilesWithFolder() async {
    var result = await SaveToCache.saveFilesWithFolder(
      files: [fileSelect],
      folderName: 'test_files',
      //names: ["test1"],
    );
    _showMessage('Successful! $result');
  }
}
1
likes
130
pub points
10%
popularity

Publisher

verified publishersondv.dev

This package is used to cache files, can save files individually and in folders.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

flutter, path_provider

More

Packages that depend on save_to_cache