listFiles method

  1. @override
Future<List<CloudFile>> listFiles({
  1. String path = '',
  2. bool recursive = false,
})
override

Lists all files and directories at the specified path.

Implementation

@override
Future<List<CloudFile>> listFiles(
    {String path = '', bool recursive = false}) {
  return _executeRequest(() async {
    final List<CloudFile> allFiles = [];
    String? cursor;
    bool hasMore = true;
    String initialPath = path == '/' ? '' : _normalizePath(path);
    debugPrint(
        'Listing files in Dropbox path: "$initialPath", recursive: $recursive');
    // Paginate through results using the cursor until all files are fetched.
    while (hasMore) {
      Response response;
      if (cursor == null) {
        // First request.
        response = await _dio.post(
          'https://api.dropboxapi.com/2/files/list_folder',
          data: jsonEncode(
              {'path': initialPath, 'recursive': recursive, 'limit': 1000}),
          options: Options(contentType: 'application/json'),
        );
      } else {
        // Subsequent paged requests.
        debugPrint('Fetching next page of files with cursor...');
        response = await _dio.post(
          'https://api.dropboxapi.com/2/files/list_folder/continue',
          data: jsonEncode({'cursor': cursor}),
          options: Options(contentType: 'application/json'),
        );
      }
      final entries = response.data['entries'] as List;
      allFiles.addAll(
          entries.map((e) => _mapToCloudFile(e as Map<String, dynamic>)));
      hasMore = response.data['has_more'] as bool;
      cursor = response.data['cursor'] as String?;
    }
    debugPrint('Found ${allFiles.length} files/folders in "$initialPath".');
    return allFiles;
  });
}