Advertisement
Kocyk

Untitled

Mar 29th, 2023
941
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.35 KB | None | 0 0
  1.  Future<void> loadFromXML() async {
  2.     try {
  3.       //emit(const ComponentsState.loading());
  4.       final File file = File('katalog.xml');
  5.       final String content = file.readAsStringSync();
  6.       final XmlDocument doc = XmlDocument.parse(content);
  7.       final laptopsNode = doc.findAllElements('laptop').toList();
  8.  
  9.       List<TextEditingController> controllers = [];
  10.       List<String> lines = [];
  11.  
  12.       for (var laptop in laptopsNode) {
  13.         String s = '';
  14.         s += laptop.findElements('manufacturer').first.text;
  15.         s += ';';
  16.         s += laptop.findAllElements('size').first.text;
  17.         s += ';';
  18.         s += laptop.findAllElements('resolution').first.text;
  19.         s += ';';
  20.         s += laptop.findAllElements('type').first.text;
  21.         s += ';';
  22.         s += laptop.findAllElements('screen').first.getAttribute('touch')!;
  23.         s += ';';
  24.         s += laptop.findAllElements('procesor').first.getElement('name')!.text;
  25.         s += ';';
  26.         s += laptop.findAllElements('physical_cores').first.text;
  27.         s += ';';
  28.         s += laptop.findAllElements('clock_speed').first.text;
  29.         s += ';';
  30.         s += laptop.findAllElements('ram').first.text;
  31.         s += ';';
  32.         s += laptop.findAllElements('storage').first.text;
  33.         s += ';';
  34.         s += laptop.findAllElements('disc').first.getAttribute('type')!;
  35.         s += ';';
  36.         s += laptop
  37.             .findAllElements('graphic_card')
  38.             .first
  39.             .getElement('name')!
  40.             .text;
  41.         s += ';';
  42.         s += laptop.findAllElements('memory').first.text;
  43.         s += ';';
  44.         s += laptop.findAllElements('os').first.text;
  45.         s += ';';
  46.         s += laptop.findAllElements('disc_reader').first.text;
  47.  
  48.         lines.add(s);
  49.       }
  50.  
  51.       for (var line in lines) {
  52.         if (!line.contains(';')) {
  53.           throw Exception();
  54.         }
  55.         var tab = line.split(';');
  56.  
  57.         for (var c in tab) {
  58.           controllers.add(
  59.             TextEditingController(
  60.                 text: c.trim().isNotEmpty ? c : 'brak danych'),
  61.           );
  62.         }
  63.       }
  64.  
  65.       emit(ComponentsState.loaded(
  66.         componentsList: lines,
  67.         controllers: controllers,
  68.       ));
  69.     } catch (error) {
  70.       log('Error while loading data', error: error);
  71.       emit(const ComponentsState.initial());
  72.     }
  73.   }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement