iterSub method

Iterable<AssetLookup> iterSub({
  1. bool folders = true,
  2. bool files = true,
  3. List<String> patterns = const [],
})

Iterates through all AssetLookup objects in this folder and its subfolders

Parameters:

  • folders: Whether to include folders in the iteration (defaults to true)
  • files: Whether to include files in the iteration (defaults to true)
  • patterns: List of glob patterns to filter results (empty list means no filtering)

Returns: An iterable of AssetLookup objects that match the criteria

Example:

Get all files with .json extension

for (final asset in folder.iterSub(folders: false, patterns: ['*.json'])) {
  if (asset is AssetLookupFile) {
    print('Found JSON: ${asset.basename}');
  }
}

Get all folders containing 'data' in the name

for (final asset in folder.iterSub(files: false, patterns: ['*data*'])) {
  print('Found folder: ${asset.path}');
}

Implementation

Iterable<AssetLookup> iterSub({
  bool folders = true,
  bool files = true,
  List<String> patterns = const [],
}) sync* {
  // Process current folder's children
  for (final child in children) {
    bool shouldYield = false;

    if (child is AssetLookupFolder && folders) {
      shouldYield = true;
    } else if (child is AssetLookupFile && files) {
      shouldYield = true;
    }

    // Apply pattern filtering if patterns are provided
    if (shouldYield && patterns.isNotEmpty) {
      shouldYield = patterns.any(
        (pattern) => _matchesPattern(child.normalizedPath, pattern),
      );
    }

    if (shouldYield) {
      yield child;
    }

    // Recursively process subfolders
    if (child is AssetLookupFolder) {
      yield* child.iterSub(
        folders: folders,
        files: files,
        patterns: patterns,
      );
    }
  }
}