fetchTemplates method
Implementation
Future<List<Template>> fetchTemplates() async {
if (_baseUrl == null || _companyId == null) {
throw Exception('Service not initialized. Call initialize() first.');
}
try {
// Create base URI
final baseUri = Uri.parse(_baseUrl!);
// Manually construct query string to avoid double encoding
final queryString = 'companyId=${Uri.encodeComponent(_companyId!)}';
final uri = Uri.parse('${baseUri.toString()}?$queryString');
debugPrint('Fetching templates from: ${uri.toString()}');
final response = await http.get(uri);
if (response.statusCode == 200) {
List<dynamic> jsonData = json.decode(response.body);
debugPrint('API Response: ${response.body}');
return jsonData.map((json) => Template.fromJson(json)).toList();
} else {
throw Exception('Failed to load templates: ${response.statusCode}');
}
} catch (e) {
throw Exception('Failed to fetch templates: $e');
}
}