R-n Phuyal
R-n Phuyal

Reputation: 53

How do i list files in flutter?

import 'dart:io';
Directory dir = Directory('/storage/emulated/0');
List<FileSystemEntity> file = dir.listSync();

I had used that code to list file which only list directory. How can i get list of files in that directory? And how to find permission of files/folder?

Upvotes: 1

Views: 5138

Answers (1)

Kirill Bubochkin
Kirill Bubochkin

Reputation: 6343

listSync method lists both directories and files. If you need only files, you can filter the output:

dir.listSync().where((e) => e is File);

If you need to list files recursively in sub-directories, you can call it with recursive parameter:

dir.listSync(recursive: true).where((e) => e is File);

And how to find permission of files/folder?

dir.listSync().forEach((e) {
  final mode = e.statSync().mode;
});

mode contains the mode of the file system object.

Permissions are encoded in the lower 16 bits of this number, and can be decoded using the modeString getter.

Upvotes: 7

Related Questions