I would like to use Flutter to access a specific folder on windows.
Flutter has a package called File Picker, but I could only select a file using this package.
There is also win32 package, but it seems like it is too technical for me to understand.
For example, I have a folder -> C:\test.
I need to view all files in this folder (C:\test) preferably in listView or table format using flutter.
How would I do this?
Thank You
If you just want to list all files name that existed in your targeted folder, you can do it without file_picker
package. There's dart:io
that provide this need.
Import dart:io
by place this import on the top of your code
import 'dart:io';
First create this async function getDirList()
Future<List> getDirList() async {
// Initiate your directory target.
//
// Make sure your path is correct.
final Directory dir = Directory('your path');
// Create variable to insert the files path
List files = [];
// Retrieve files path that existed in your directory target.
//
// fyi: [dir.list()] using stream not future.
await for (var entity in dir.list()) {
// Original value of [entity.path] is String of full path of file, so i manipulate
// String to only show the name of the file.
//
// If you want to the original value just use:
// `entity.path`
// That variable will returned a String.
files.add(entity.path.split('/').last);
}
return files;
}
then apply the async function using FutureBuilder
:
FutureBuilder<List>(
future: getDirList(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Column(
children: snapshot.data!
.map((value) => Padding(
padding: const EdgeInsets.all(16),
child: Text('- $value'),
))
.toList(),
);
} else if (snapshot.hasError) {
return Center(child: Text('error: ${snapshot.error}'));
}
return const Center(child: CircularProgressIndicator());
},
)
Im using
Column
to show the data, you can modify it if you want to show inListView
orTable
Additional
If you want to check your folder is correct (exist) you can check it by:
// [dir] is the variable that you initiated before in [getDirList()] function
final bool isDirExist = await dir.exists();
print ('is directory exist $isDirExist');
It will return true
if it's exist.