Need Help getting the list of files in a directory and updating it.

1,123 views
Skip to first unread message

Jake Rener

unread,
Apr 14, 2020, 3:43:58 AM4/14/20
to Flutter Development (flutter-dev)
I need to get a list of all the files in a particular directory in /sdcard/ and display them on my app. The list needs to updated when a new file is added or deleted from the folder.
I have tried using the Directory method Directory.list() to get a stream of files and used the stream in a StreamBuilder widget but I only get one file repeatedly in the stream.
Here's the code: 

class HomeScreen extends StatefulWidget {
 
@override
  _HomeScreenState createState
() => _HomeScreenState();
}


class _HomeScreenState extends State<HomeScreen> {
 
final booksDir = new Directory('/sdcard/Genesis Reader');
 
bool isDirectoryExistent = false;
 
List<String> bookPaths = [];
 
Stream fileStream;


 
final RegExp re = RegExp(r'(?<=Reader\/)(.*)(?=\.epub)');


 
@override
 
void initState() {
   
super.initState();
    getDirStatus
();
   
// EpubKitty.setConfig("book", "#232931", "vertical", true);
 
}


 
void getDirStatus() async {
    await booksDir
.exists().then((isThere) {
     
if (isThere) {
        setState
(() {
          isDirectoryExistent
= true;
       
});
        fileStream
= booksDir.list();
     
}
   
});
 
}


 
@override
 
Widget build(BuildContext context) {
   
// print(bookPaths);
   
return BaseScaffold(
      subHeading
: 'Downloaded Books',
      searchWidget
: SearchBar(),
      mainWidget
: isDirectoryExistent
         
? StreamBuilder(
              stream
: fileStream,
              builder
: (context, snapshot) {
               
if (snapshot.hasData) {
                 
print('from print: ${snapshot.data}');
                 
// return Text(snapshot.data.path);
               
}
               
return CircularProgressIndicator();
             
})
         
: NoBooks(),
   
);
 
}
}
What I need is to be able get a stream of files and then use StreamBuilder to list all the files in that directory

Suzuki Tomohiro

unread,
Apr 14, 2020, 7:54:38 AM4/14/20
to Flutter Development (flutter-dev)
You may want to use watch rather than list. List cannot handle changes.

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/628f6129-703a-4019-b4f5-981afb95eaa6%40googlegroups.com.

Jake Rener

unread,
Apr 14, 2020, 8:16:40 AM4/14/20
to Flutter Development (flutter-dev)
Using FileSystemEntity.watch causes : 
ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: FileSystemException: File system watching is not supported on this platform, path = ''
I tried this: 
String path = 'path/to/directory';
Directory dir = Directory(path);    
var stream = dir.watch(events: FileSystemEvent.all);
stream
.listen((data){
 
print(data.path);
});
To unsubscribe from this group and stop receiving emails from it, send an email to flutt...@googlegroups.com.

Suzuki Tomohiro

unread,
Apr 14, 2020, 8:27:30 AM4/14/20
to Flutter Development (flutter-dev)
And your platform is?

> The implementation uses platform-dependent event-based APIs for receiving file-system notifications, thus behavior depends on the platform.

  • Windows: Uses ReadDirectoryChangesW. The implementation only supports watching directories. Recursive watching is supported.
  • Linux: Uses inotify. The implementation supports watching both files and directories. Recursive watching is not supported. Note: When watching files directly, delete events might not happen as expected.
  • OS X: Uses FSEvents. The implementation supports watching both files and directories. Recursive watching is supported.


To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/c6749232-6c2c-4ef4-8523-a20d36be234a%40googlegroups.com.

Jake Rener

unread,
Apr 14, 2020, 8:36:41 AM4/14/20
to Flutter Development (flutter-dev)
My platform is Linux. I am using Kubuntu 18.04 LTS. Just wondering, how is platform relevant to the android emulator or in my case android phone connected externally?

Suzuki Tomohiro

unread,
Apr 14, 2020, 8:49:37 AM4/14/20
to Flutter Development (flutter-dev)
I have zero idea why it does not work. You can use debugger and breakpoint to see what platform your dart VM recognizes the environment before throwing the “watching is not supported” error.

To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/2e5ce7d1-ecc7-4a2d-a6aa-d212e9515ca6%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages