Advertisement
Guest User

FileRecursion

a guest
Feb 20th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 0.77 KB | None | 0 0
  1. class File {
  2.   static const int FOLDER = 1;
  3.   static const int FILE = 2;
  4.  
  5.   int type;
  6.   String name;
  7.   List<File> children = [];
  8.  
  9.   File();
  10.  
  11.   List<String> findFile(String pattern, [String start = '']) {
  12.     final List<String> ret = [];
  13.  
  14.     final newStart = start.isNotEmpty ? '$start/$name' : name;
  15.     if (name.contains(pattern)) {
  16.       ret.add(newStart);
  17.     }
  18.  
  19.     for (final el in children) {
  20.       var x = el.findFile(pattern, newStart);
  21.       ret.addAll(x);
  22.     }
  23.  
  24.     return ret;
  25.   }
  26. }
  27.  
  28. void main() {
  29.   final f = File()
  30.     ..name = ''
  31.     ..children = [
  32.       File()
  33.         ..name = 'pesho'
  34.         ..children = [File()..name = 'code.java'],
  35.       File()..name = 'code2.java'
  36.     ];
  37.  
  38.   final res = f.findFile('.java');
  39.   print(res);
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement