Like in Instagram, we can save stories, and reels on our devices. We also want to save some information or some file on our device in android we will keep the file from the flutter app on android devices.
Step By Step Implementation
Step 1: Create a New Project in Android Studio
To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.
Step 2: Add these packages to your project
path_provider: permission_handler:
Note: If you are using visual studio use can use ctrl+shift+p in windows & ctrl+shift+p in macOS –>Add dependency and type the package you want to add.
Step 3: Add these permissions to the Androidmanifest.xml file
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Step 4: Get the data you need to store in a local device
const myList = {
"name" : "GeeksForGeeks"
}; |
Step 5: Create 1 button where you can call the function for storing the data
floatingActionButton: FloatingActionButton( onPressed: (){
},
tooltip: 'Save File' ,
child: const Icon(Icons.save),
),
|
Step 6: Now create 1 class where we will define all the functions to store the file
class FileStorage{}
|
Step 7: Copy and paste this code containing all functions
import 'dart:io' ;
import 'package:path_provider/path_provider.dart' ;
import 'package:permission_handler/permission_handler.dart' ;
// To save the file in the device class FileStorage {
static Future<String> getExternalDocumentPath() async {
// To check whether permission is given for this app or not.
var status = await Permission.storage.status;
if (!status.isGranted) {
// If not we will ask for permission first
await Permission.storage.request();
}
Directory _directory = Directory( "" );
if (Platform.isAndroid) {
// Redirects it to download folder in android
_directory = Directory( "/storage/emulated/0/Download" );
} else {
_directory = await getApplicationDocumentsDirectory();
}
final exPath = _directory.path;
print( "Saved Path: $exPath" );
await Directory(exPath).create(recursive: true );
return exPath;
}
static Future<String> get _localPath async {
// final directory = await getApplicationDocumentsDirectory();
// return directory.path;
// To get the external path from device of download folder
final String directory = await getExternalDocumentPath();
return directory;
}
static Future<File> writeCounter(String bytes,String name) async {
final path = await _localPath;
// Create a file for the path of
// device and file name with extension
File file= File( '$path/$name' );;
print( "Save file" );
// Write the data in the file you have created
return file.writeAsString(bytes);
}
} |
Step 8: Will call the function of the button we have created in step 5
floatingActionButton: FloatingActionButton( onPressed: (){
// First parameter will data you want store .
// It must be a in form of string
// Second parameter will be the file name with extensions.
FileStorage.writeCounter(
myList.toString(), "geeksforgeeks.txt" );
},
tooltip: 'Save File' ,
child: const Icon(Icons.save),
),
|
Output:
1. It will ask for permission to read and write in external storage
2. Then it will save the file and after completing the process it will show SnackBar like this
3. File save in the downloads folder
4. When we open the file