parseNotifications function

List<Map<String, dynamic>> parseNotifications(
  1. String response
)

Implementation

List<Map<String, dynamic>> parseNotifications(String response) {
  try {
    // The response should be a JSON array
    final decoded = jsonDecode(response.trim());
    if (decoded is List) {
      return decoded.cast<Map<String, dynamic>>();
    } else {
      return [];
    }
  } catch (e) {
    // Try parsing line by line as fallback
    final notifications = <Map<String, dynamic>>[];
    final lines = response.split('\n');

    for (final line in lines) {
      if (line.trim().isEmpty || line.startsWith('data:')) continue;

      try {
        final decoded = jsonDecode(line);
        if (decoded is Map<String, dynamic>) {
          notifications.add(decoded);
        }
      } catch (e) {
        // Skip lines that can't be parsed as JSON
      }
    }

    return notifications;
  }
}