findFileByName function

Future<File?> findFileByName(
  1. String fileName,
  2. String directoryPath
)

Finds a file with the given fileName in the directory located at directoryPath. Returns null if the directory does not exist or if the

Implementation

Future<File?> findFileByName(String fileName, String directoryPath) async {
  final directory = Directory(directoryPath);
  if (!await directory.exists()) return null;
  final entities = directory.listSync(recursive: true);
  for (final entity in entities) {
    if (entity is File && entity.path.endsWith('/$fileName')) {
      return entity;
    }
  }
  return null;
}