In my project, I want to get the number of files in a directory when a button is pressed or tapped. This functionality seems to work in Dart standalone code, but when called in the onTap function it then throws an error indicating that the path does not exist or is empty. However, the path exists and is not empty and it is listed in the pubspec.yaml file.
This list the number of files available
void main(){
getNumberOfFiles();
}
Future<int> getNumberOfFiles() async {
String bookPath = 'assets/books/jude/';
final dir = Directory(bookPath);
if (!await dir.exists()) {
print('Directory does not exist: $bookPath');
return 0;
}
final fileList = await dir.list().toList();
final fileCount = fileList.whereType<File>().length;
print('Number of files: $fileCount');
return fileCount;
}
This one down here throws error
onTap: () async {context.read<BibleTextManager>().getNumberOfFiles();},
I am using the provider because the function was and still in a provider class but I just took the method out to another file to test with the main function.