String safeSubstring(String str, int start, int end) { final len = str.length; if (len > start) { final lastIndex = min(len, end); return str.substring(start, lastIndex); } return ''; }