Flutter File Save In Folder

Flutter File Save In Folder :

dependencies:
folder_file_saver:

permission set AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Flutter File Save In Folder
import 'package:flutter/material.dart';
import 'package:folder_file_saver/folder_file_saver.dart';
import 'package:path_provider/path_provider.dart' as p;
import 'package:dio/dio.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String progress = "0";
  bool _isLoading = false;
  final urlVideo = '', urlImage ='';
  Dio dio;
  @override
  void initState() {
    super.initState();
    dio = Dio();
  }

  void _saveImage() async {
    await FolderFileSaver.getPermission().then((statusPermission) async {
      if (statusPermission == 0) {
        setState(() {
          _isLoading = true;
        });
        String result;
        final dir = await p.getTemporaryDirectory();
        final pathImage = dir.path + ('example_image.png');
        try {
          await dio.download(urlImage, pathImage,
              onReceiveProgress: (rec, total) {
            setState(() {
              progress = ((rec / total) * 100).toStringAsFixed(0) + "%";
            });
          });
          // if you want to get original of Image
          // don't give a value of width or height
          // cause default is return width = 0, height = 0
          // which will make it to get the original image
          // just write like this
          result = await FolderFileSaver.saveImage(pathImage: pathImage);
        } catch (e) {
          result = e;
        }
        print(result);
        setState(() {
          _isLoading = false;
        });
      }
    });
  }

  void _saveFolderFileExt() async {
    // if you want check permission user
    // use like that
    // if return 0 permission is PERMISSION_GRANTED
    // if return 1 permission is PERMISSION_IS_DENIED
    // if return 2 permission is PERMISSION_IS_DENIED with click don't ask again
    await FolderFileSaver.getPermission().then((statusPermission) async {
      if (statusPermission == 0) {
        setState(() {
          _isLoading = true;
        });
        String result;
        final dir = await p.getTemporaryDirectory();
        // prepare the file and type extension that you want to download
        final filePath = dir.path + ('example_video.mp4');
        try {
          await dio.download(urlVideo, filePath,
              onReceiveProgress: (rec, total) {
            setState(() {
              progress = ((rec / total) * 100).toStringAsFixed(0) + "%";
            });
          });
          result = await FolderFileSaver.saveFileToFolderExt(filePath);
        } catch (e) {
          result = e;
        }
        print(result);
        setState(() {
          _isLoading = false;
        });
      }
    });
  }

  //permission
  void saveFileNotCheckPermission() async {
    String result;
    final dir = await p.getTemporaryDirectory();
    // prepare the file and type extension that you want to download
    final filePath = dir.path + ('example_video.mp4');
    try {
      await dio.download(urlVideo, filePath);
      result = await FolderFileSaver.saveFileToFolderExt(filePath);
    } catch (e) {
      result = e;
    }
    print(result);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Folder File Saver'),
          centerTitle: true,
        ),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              onPressed: _isLoading ? null : _saveImage,
              child:
                  Text(_isLoading ? 'Downloading $progress' : 'Download File'),
            ),
            RaisedButton(
              onPressed: _isLoading ? null : _saveFolderFileExt,
              child:
                  Text(_isLoading ? 'Downloading $progress' : 'Download File'),
            ),
            RaisedButton(
              onPressed: () async => await FolderFileSaver.openSetting,
              child: Text('Open Setting App'),
            ),
          ],
        ),
      ),
    );
  }
}

The flutter tutorial  is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application

For more information about Flutter. visit www.fluttertutorial.in