Flutter app hangs when parsing large input in custom CodeParser

I’m developing a Flutter application that uses a custom CodeParser to parse input and update a CoordinateSystemModel. The parser works fine for small inputs, but it hangs and becomes unresponsive when dealing with larger inputs. Here’s a simplified version of my CodeParser:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class CodeParser {
static void parseCode(String code, CoordinateSystemModel model) {
print('Parsing code: $code');
List<String> lines = code.split('n');
List<Place> newPlaces = [];
List<Task> newTasks = [];
for (String line in lines) {
line = line.trim();
parseAndExecuteLine(line, model, newPlaces, newTasks);
}
model.clearAndReplacePlaces(newPlaces);
model.clearAndReplaceTasks(newTasks);
}
static void parseAndExecuteLine(String line, CoordinateSystemModel model,
[List<Place>? newPlaces, List<Task>? newTasks]) {
if (line.isEmpty) return;
if (line.startsWith('project')) {
_parseProject(line, model);
} else if (line.startsWith('type')) {
_parseType(line, model);
} else if (line.startsWith('range')) {
_parseRange(line, model);
} else if (line.startsWith('path')) {
_parsePath(line, model);
} else if (line.startsWith('place')) {
Place? place = _parsePlace(line);
if (place != null) {
if (newPlaces != null) {
newPlaces.add(place);
} else {
model.addPlace(place);
}
}
} else if (line.startsWith('task')) {
Task? task = _parseTask(line);
if (task != null) {
if (newTasks != null) {
newTasks.add(task);
} else {
model.addTask(task);
}
}
}
}
// Other parsing methods...
}
</code>
<code>class CodeParser { static void parseCode(String code, CoordinateSystemModel model) { print('Parsing code: $code'); List<String> lines = code.split('n'); List<Place> newPlaces = []; List<Task> newTasks = []; for (String line in lines) { line = line.trim(); parseAndExecuteLine(line, model, newPlaces, newTasks); } model.clearAndReplacePlaces(newPlaces); model.clearAndReplaceTasks(newTasks); } static void parseAndExecuteLine(String line, CoordinateSystemModel model, [List<Place>? newPlaces, List<Task>? newTasks]) { if (line.isEmpty) return; if (line.startsWith('project')) { _parseProject(line, model); } else if (line.startsWith('type')) { _parseType(line, model); } else if (line.startsWith('range')) { _parseRange(line, model); } else if (line.startsWith('path')) { _parsePath(line, model); } else if (line.startsWith('place')) { Place? place = _parsePlace(line); if (place != null) { if (newPlaces != null) { newPlaces.add(place); } else { model.addPlace(place); } } } else if (line.startsWith('task')) { Task? task = _parseTask(line); if (task != null) { if (newTasks != null) { newTasks.add(task); } else { model.addTask(task); } } } } // Other parsing methods... } </code>
class CodeParser {
  static void parseCode(String code, CoordinateSystemModel model) {
    print('Parsing code: $code');
    List<String> lines = code.split('n');
    List<Place> newPlaces = [];
    List<Task> newTasks = [];

    for (String line in lines) {
      line = line.trim();
      parseAndExecuteLine(line, model, newPlaces, newTasks);
    }

    model.clearAndReplacePlaces(newPlaces);
    model.clearAndReplaceTasks(newTasks);
  }

  static void parseAndExecuteLine(String line, CoordinateSystemModel model,
      [List<Place>? newPlaces, List<Task>? newTasks]) {
    if (line.isEmpty) return;

    if (line.startsWith('project')) {
      _parseProject(line, model);
    } else if (line.startsWith('type')) {
      _parseType(line, model);
    } else if (line.startsWith('range')) {
      _parseRange(line, model);
    } else if (line.startsWith('path')) {
      _parsePath(line, model);
    } else if (line.startsWith('place')) {
      Place? place = _parsePlace(line);
      if (place != null) {
        if (newPlaces != null) {
          newPlaces.add(place);
        } else {
          model.addPlace(place);
        }
      }
    } else if (line.startsWith('task')) {
      Task? task = _parseTask(line);
      if (task != null) {
        if (newTasks != null) {
          newTasks.add(task);
        } else {
          model.addTask(task);
        }
      }
    }
  }

  // Other parsing methods...
}

The parser works fine with small inputs, but it hangs when I try to parse larger inputs.

I’ve tried optimizing the parser by implementing a chunked approach and adding batch methods to my CoordinateSystemModel:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>void addPlaces(List<Place> newPlaces) {
_places.addAll(newPlaces);
_places.sort((a, b) => a.km.compareTo(b.km));
_updateCodeAndNotify();
}
void addTasks(List<Task> newTasks) {
_tasks.addAll(newTasks);
_tasks.sort((a, b) => a.startDate.compareTo(b.startDate));
_updateCodeAndNotify();
}
</code>
<code>void addPlaces(List<Place> newPlaces) { _places.addAll(newPlaces); _places.sort((a, b) => a.km.compareTo(b.km)); _updateCodeAndNotify(); } void addTasks(List<Task> newTasks) { _tasks.addAll(newTasks); _tasks.sort((a, b) => a.startDate.compareTo(b.startDate)); _updateCodeAndNotify(); } </code>
void addPlaces(List<Place> newPlaces) {
  _places.addAll(newPlaces);
  _places.sort((a, b) => a.km.compareTo(b.km));
  _updateCodeAndNotify();
}

void addTasks(List<Task> newTasks) {
  _tasks.addAll(newTasks);
  _tasks.sort((a, b) => a.startDate.compareTo(b.startDate));
  _updateCodeAndNotify();
}

However, the app still becomes unresponsive with larger inputs. How can I optimize my CodeParser to handle larger inputs without hanging? Any suggestions for improving performance or alternative approaches would be greatly appreciated.

Environment:

Flutter 3.22.3 • channel stable • https://github.com/flutter/flutter.git
Framework • revision b0850beeb2 (5 weeks ago) • 2024-07-16 21:43:41 -0700
Engine • revision 235db911ba
Tools • Dart 3.4.4 • DevTools 2.34.3

Browser: Google Chrome
OS: macOS

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật