fetchLanguages method

Future<List<LanguageModel>> fetchLanguages()

Implementation

Future<List<LanguageModel>> fetchLanguages() async {
  // 1. Load the JSON string from the assets folder
  const String response = languageJsonString;

  // 2. Check for loading errors (optional, but good practice)
  if (response.isEmpty) {
    throw Exception('Error loading JSON file');
  }

  // 3. Decode the JSON string into a Dart object
  final data = await json.decode(response) as List<
      dynamic>; // Cast to List<dynamic> to avoid potential type errors

  // 4. Convert each JSON object to a LanguageModel instance
  return data.map((item) => LanguageModel.fromJson(item)).toList();
}